Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6821375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:32:17+00:00 2026-05-26T21:32:17+00:00

I am using JavaScript Date object to convert milliseconds into a readable minutes:seconds formatted

  • 0

I am using JavaScript Date object to convert milliseconds into a readable minutes:seconds formatted string. I need that to create a timer for a custom video player, where my JS interface receives the video duration info as a millisecond value.

It’s been a fairly trivial task, before I decided to support a possibility of having a video longer than 59 minutes. And then I encountered this problem: when I submit a millisecond value to the new Date object’s constructor, and then call getHours(), it will return something even if the number of milliseconds stands for a period of time less than an hour. The easiest way to see this in action is to feed it, say, 0.

enter image description here

I‘d expect it to return 0, but it always returns 12 (13 in Opera, which makes it even more weird). Is this a normal behaviour or is it a bug? Either way, how do I reliably detect whether my millisecond value is limited to just minutes and seconds or also includes hours?

Thanks.

UPD:

I‘ve tested it in Chrome 15 and Firefox 7 on OSX: same result as per the screenshot above. I cannot figure out how to use Opera Dragonfly console, but from what I see same thing happens in Opera, just with the value of 13 for getHours().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:32:18+00:00Added an answer on May 26, 2026 at 9:32 pm

    Try calling new Date(0).toString() and see if there is a timezone affecting it?

    Mine gives

    "Thu Jan 01 1970 02:00:00 GMT+0200 (FLE Standard Time)"
    

    Because I am on GMT+2, so getHours gives 2 to me.

    The Unix epoch is GMT+0

    Use +new Date to get reliable milliseconds at a point in time and keep it as number. Do not do any math within an actual date object.

    Here is what I have used:

    var toTimeString = function( secs ) {
    var days, hours, minutes, seconds;
    
        if( secs == null )
        return "";
    
    days = ( secs / 86400 ) >> 0;
    hours = ( secs % 86400 / 3600 ) >> 0;
    minutes = ( secs % 3600 / 60 ) >> 0;
    seconds = ( secs % 60 );    
    seconds = seconds < 10 ? "0" + seconds : seconds;
    minutes = minutes < 10 ? "0" + minutes : minutes;
    hours = hours && hours < 10 ? "0" + hours : hours;
    
    return "" + ( days ? days+" - " : "" ) + ( hours ? hours+":" : "" ) + minutes + ":" + seconds;      
    };
    

    Apply custom formatting as required.. This actually takes seconds so divide milliseconds by 1000 to get seconds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to parse a string into a date object at JavaScript (without using any
My goal is to convert a timestamp from MySQL into a JavaScript Date object
When initializing a new Date object in JavaScript using the below call, I found
I've been using Javascript's Date for a project, but noticed today that my code
Using JavaScript, how do I create an HTML table that can "accept" numeric matrix
I am speed testing some JavaScript programs by creating a Date object, and using
I've seen this: how to get formatted date time like 2009-05-29 21:55:57 using javascript?
I would like to convert a string to date object and add in the
i am trying to convert a string in the format dd-mm-yyyy into a date
I'm using a script calendar that when I choose a date, it convert it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.