Created
September 30, 2010 21:41
-
-
Save desandro/605373 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}; |
@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
@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:
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.