Skip to content Skip to sidebar Skip to footer

Mongodb + Js Date Shifted -1 Day

I'm trying to add a new date inside my mongodb document using db.collection('course').insertOne({ date: new Date(item.date) }); item.date is from items.push({ date: moment

Solution 1:

It's not shifted by a day, it's not actually shifted at all.

The time shown is shifted by an hour. If you notice, the time has become 23:00 on the previous day, rather than defaulting to midnight on the date you have specified.

More importantly, the Z suffix in the timestamp means that the timezone is UTC, and 2016-11-13T23:00:00 in UTC refers to the same moment in time as the timestamp 2016-11-14T00:00:00+01:00. So the datetime is being created correctly, but just shown back to you in a different timezone that you're creating it in.

Solution 2:

if you want proper solution for all date UTC problem, store date as standard ISO format

// Indiamoment().toISOString() // "2016-11-06T06:17:33.520Z"// canadamoment().toISOString() // "2016-11-06T06:19:42.133Z"

now see the diffrence by convering date from database

// canada time zonemoment("2016-11-06T06:19:42.133Z").format() // "2016-11-05T23:19:42-07:00"// india time zone moment("2016-11-06T06:19:42.133Z").format() //"2016-11-06T11:49:42+05:30"

as above see the diffrence of local UTC, so don't have to convert date from any timezone to other timezone

Post a Comment for "Mongodb + Js Date Shifted -1 Day"