At the time of this post my current time is 2017-01-10T19:23:00.000Z
but new Date()
gives me 2017-01-11T00:23:19.521Z
5 hours ahead of my current timezone. This affects the way my data is stored in my MongoDB. I know I can set the time to 5 hours ago using
var datetime = new Date();
datetime.setHours(datetime.getHours()-5);
But I will prefer a better way to do this. I tried using this. I still got the same time. In other parts of my code I get Tue Jan 10 2017 19:54:30 GMT-0500 (EST)
different from the initial time. I will be happy if someone can point out what's wrong here.
new Date().toLocaleString();
and I got1/10/2017, 8:20:30 PM
not exactly what I'm looking for. I will like the time inTZ
format. Howevernew Date(Date.UTC(year, month, day, hour, minute, second))
gave an error ofyear is not defined
– Frequent2017-01-11 00:56:01Z
. No idea where you lost one and a half hours? – PermissionZ
means). The one is formatted using.toUTCString()
(or.toISOString()
), the other is formatted using.toString()
which does output your local timezone. They might be represented by the sameDate
object; and no, this does not affect how they are stored in MongoDB. – Permission{ "_id" : ObjectId("58758246b67c161ac8479489"), "datetime" : ISODate("2017-01-11T00:54:30.241Z"), "temperature" : 19, "humidity" : 23 }
– FrequentDate
object is just a number (milliseconds since epoch). You need to parse and format appropriately for the timezone you want. – Permission