Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
Javascript Dates working weird (when converting milliseconds to seconds )

I have future date and now date. Both of this dates are always in same day but with just different hours. My goal is to get the difference of seconds between the future date and now date as my countdown value for a timer. The problem is when I calculate I'm getting inaccurate results.

In my research formula of converting milliseconds to seconds is millis / 1000.0 but non of this returns accurate countdown result;

My code

let now = (new Date().getTime() / 1000.0);
let futureDate = (new Date('2022-04-01T17:41:47.000Z').getTime() / 1000.0);

let difference;
difference = (futureDate - now); // not accurate
difference = parseInt(difference, 10); // not accurate

I would like the solution to work normal on all timezones and to inherit local system timezone instead of future date timezone.

Any help will be appreciated so much.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I want to check if you know that date formats like "0000-00-00T00:00:00.000Z" are always recognized as universal time (UK time).

Try using +HH:MM instead of the last Z character.

For example, if you are in the US East, it would be "2022-04-01T17:41:47.000-05:00".

about 4 years ago · Juan Pablo Isaza Report

0

You should add the system timezone, like this:

let date = new Date('2022-04-01T17:41:47.000Z');
let now = new Date().getTime() / 1000.0;
let futureDate = (date.getTime() + date.getTimezoneOffset() * 60000) / 1000.0;

let difference;
difference = (futureDate - now); // not accurate
difference = parseInt(difference, 10); // not accurate
console.log(difference);

about 4 years ago · Juan Pablo Isaza Report

0

The timestamp '2022-04-01T17:41:47.000Z' will be parsed as offset +0 (aka UTC or GMT) due to the trailing "Z", denoting a +0 offset. The difference between now and then in milliseconds is:

let diff = new Date('2022-04-01T17:41:47.000Z') - Date.now();

where a negative value for diff means the timestamp is in the past. To convert the difference to seconds, divide the result by 1,000.

If run at exactly the same time, the value for diff will be the same regardless of system settings for local offset (allowing for clock accuracy of course).

For the timestamp to be parsed as local, remove the Z:

let diff = new Date('2022-04-01T17:41:47.000') - Date.now();

However, that shifts the instant in time represented by the timestamp by the local offset, so will return a different value for diff for each system with a different offset. That doesn't seem like a sensible thing to do given the timestamp has an offset and so is intended to represent a single instant in time, not one of many different instants (as many as there are different offsets, perhaps hundreds if historical offsets are included) depending on the host offset.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!