I have this react typescript/js code:
JavaScript
x
2
1
fromDate: fromDate.toISOString().split('T')[0], // formatted to 2000-01-01
2
but it’s not working properly. It changes fromDate to the next day when I run it late at night. So I tried on 6/30/2022 at 11pm and it changed it to 7/1/2022.
My attempted fix is to use date-fns format function:
JavaScript
1
2
1
fromDate: format(Date.parse(fromDate.toLocaleString()), 'yyyy-MM-dd'),// formatted to 2000-01-01
2
My question is will this resolve the utc issue? Maybe I should rather change the datepicker that gets the fromDate to ignore times?
How can I test it without trying it at 11pm?
This image attached is how it looked in the console when I console logged the problem:
The top line is console.log(fromDate)
. The bottom line is console.log(fromDate.toISOString().split('T')[0])
Advertisement
Answer
testing was easy!
JavaScript
1
4
1
const test = new Date('Thu june 30 2022 23:03:00 GMT-0400');
2
console.log(test.toISOString().split('T')[0]);
3
console.log(format(Date.parse(test.toLocaleString()), 'yyyy-MM-dd'))
4
1st console.log shows problem 2nd console.log shows fixed problem.