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 ?
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