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

355
Views
js increment date by 3 hours when i put exact time in date
const date = new Date(2022, 4, 18, 16, 50);

Why does this return the time reduced by three hours?

I get this "2022-05-18T13:50:00.000Z" instead of this "2022-05-18T16:50:00.000Z" which I expected to get when I print this to console using console.log(date).

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

0

why does this return the time reduced by three hours?

It doesn't, it gives you exactly the time you asked for — in your local time. But then you're getting a string from it in a way that gives you a string in UTC, not local time (the Z at the end tells you it's UTC). Apparently there's a three-hour difference as of that date/time between your local timezone and UTC.

If you want to work in local time, use toLocaleString to get a string formatted according to your current locale and using your timezone. If you want a string like the one you got, but in your local timezone, as far as I know you have to build it yourself.

If you want to work in UTC, build the date using the UTC function: new Date(Date.UTC(2022, 4, 18, 16, 50));

about 4 years ago · Juan Pablo Isaza Report

0

it's because of your timezone! for example:

let d = new Date(2022, 4, 18, 16, 50);
d.toLocaleString('en-US', { timeZone: 'America/New_York' });
about 4 years ago · Juan Pablo Isaza Report

0

When you call the Date() constructor with individual components it will interpret those in your local time. See point 5 on this MDN documentation page.

But when you print int without toLocaleString() it will return the UTC time instead. Depending on your time zone this is more or less far away from what you have entered.

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!