Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts

    formatted time from seconds

    let's say you time an action in your app and it takes 215 seconds.
    you don't want to tell the user "215", you want to show them the duration in an "hh:mm:ss" format.

    the standard advice is to use a date library, but i found a much simpler and faster way:

    Code:
    var seconds = 215;
    var strTime=new Date( seconds * 1000 ).toUTCString().split(" ")[4];
    alert(strTime); // shows "00:03:35"
    it (almost) goes without saying that it only supports up to 23h59m59s max...

    short, simple, works in IE6+, and has no messy loops.
    Last edited by rnd me; 04-25-2013 at 10:09 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #2
    New Coder
    Join Date
    Oct 2008
    Posts
    45
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Arrow

    There isn't any action. Just the delay and the time.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •