Skip to content

Instantly share code, notes, and snippets.

@desandro
Created September 30, 2010 21:41
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save desandro/605373 to your computer and use it in GitHub Desktop.
Save desandro/605373 to your computer and use it in GitHub Desktop.
// converts milliseconds to '3:45' or if hours > 0, '2:01:23'
var getTimeFromMillis = function( ms ) {
var seconds = ~~( ( ms / 1000 ) % 60 ),
minutes = ~~( ( ms / ( 1000 * 60 ) ) % 60 ),
hours = ~~( ( ms / ( 1000 * 60 * 60 ) ) ),
twoDigit = function ( n ) {
return n < 10 ? '0' + n : n;
},
seconds = ':' + twoDigit( seconds );
return hours > 0 ? hours + ':' + twoDigit( minutes ) + seconds : minutes + seconds;
};
Copy link

ghost commented Sep 30, 2010

Missing a %60 for hours var?

hours = ~~( ( ms / ( 1000 * 60 * 60 ) ) % 60 )

@desandro
Copy link
Author

desandro commented Oct 1, 2010

Putting a modulus of 60 on hours would return the remainder of dividing hours by 60. So if hours = 71 % 60 would set hours as 11.

If anything, you may want to use % 24 as some proposed on the StackOverflow question and couple it with another value for days:

hours   = ~~( ( ms / ( 1000 * 60 * 60 ) ) % 24 ),
days    = ~~( ( ms / ( 1000 * 60 * 60 * 24 ) ) ),

Copy link

ghost commented Oct 1, 2010

Hmmm, my silly mistake. Sorry.

I gave it try (http://jsfiddle.net/C7mFR/1/) but for some reason it's two hours out. I'm sure it's something simple but I just can't spot it. Any ideas?

@desandro
Copy link
Author

desandro commented Oct 1, 2010

I believe this related to the time zone you're in. That fiddle returns GMT, maybe without Daylight Saving Time. There's probably an easy way to resolve the time-zone, but I haven't been able to find one yet.

@jfsiii
Copy link

jfsiii commented Oct 15, 2010

@codeinfront try http://jsfiddle.net/JFSIII/rQVqs/ and let me know if it shows the correct local time for you.

The JS Date object https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date has plenty of methods to date manipulation and presentation, but none of them are as succinct as the original gist.

Have a look at https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date#Examples to get an idea of some of the things you can do.

Copy link

ghost commented Oct 16, 2010

@jfsiii: Many thanks for your response. I've tested in IE, Safari 5, Opera 10, Firefox and Chrome and I can confirm it works for my time zone. Great work!

I think I had a bit more of a play along simliar lines but have lost the Fiddle URLs. In going back to Professional JavaScript for Web Developers by Nicholas C. Zakas this seems to be a reliable method. I assume reliability because he points out a number methods that aren't but this isn't one of them. Ref:

Several other methods are also designed to create alternate string representations of a particular date:

  • toDateString() — displays only the date part of a Date (only the month, day, and year) in an implementation-dependent format
  • toTimeString() — displays only the time part of a Date (hours, minutes, seconds, and time zone) in an implementation-dependent format
  • toLocaleString() — displays the date and time of a Date in a locale-specific format
  • toLocaleDateString() — displays the date part of a Date value in a locale-specific format
  • toLocaleTimeString() — displays the time part of a Date in a locale-specific format
  • toUTCString() — displays the UTC date of a Date in an implementation-specific format

Each of these methods outputs different values in different implementations and locales, and for this reason, care must be exercised when using them.

In case you haven’t figured it out yet, the Date class relies heavily on the UTC date and time. In order to indicate a particular time zone’s relationship to UTC, the Date class provides a method called getTimezoneOffset(). This method returns the number of minutes that the current time zone is ahead or behind UTC. For instance, getTimezoneOffset() returns 300 for U.S. Eastern Daylight Saving Time, which is 5 hours (or 300 minutes) behind UTC.

Thanks again for your response.

Copy link

ghost commented Oct 16, 2010

@jfsiii: Tell a lie, I found it. Here's my slightly more verbose offering http://jsfiddle.net/codeinfront/9BFFS/ :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment