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

104
Views
moment avoid time zone after calculation

I need to make calcul with date in moment but i just want to count days, i don't care about hours, timezone or whatever.

I store date in date without timezone, but when i made calculations with moment, I always have problem with hours changing. My computer is in France and we change hour 2 time per years.

I found some way with parseZone() and startOf('day'), but my code become very messy and i'm looking for the best way to do what i want.

For example, this code : http://jsfiddle.net/zn2pcg65/2/

var a = moment('2022-01-05T00:00:00.000Z');
console.log(a.toDate().toISOString());
var b = a.add(3,'M');
console.log(b.toDate().toISOString());

give me :

"2022-01-05T00:00:00.000Z"
"2022-04-04T23:00:00.000Z"

I wanted "2022-04-05".

What is the best way to do calculations with date without having this kind of side effect ?

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

0

The problem is that you are using ISO format, so what is ISO format

ISO: The International Organization for Standardization (ISO) date format is a standard way to express a numeric calendar date that eliminates ambiguity. For example, North Americans usually write the month before the date.

for more: https://www.techtarget.com/whatis/definition/ISO-date-format

To solve your problems you need to parse your date to UTC time.

What its etc:

Coordinated Universal Time (UTC) is the basis for civil time today. This 24-hour time standard is kept using highly precise atomic clocks combined with the Earth's rotation.

for more: https://www.timeanddate.com/time/aboututc.html

Here the solution you need:

var a = moment('2022-01-05T00:00:00.000Z').utc();
console.log('a: ',a.toDate().toISOString());
var b = a.add(3,'M');
console.log('b', b.toDate().toISOString());

//more espesific format: 
//a.format('MM-DD-YYYY') output 01-05-2022

//b.format('MM-DD-YYYY') output 04-05-2022
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!