最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - new Date() in wrong time zone - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jan 11, 2017 at 1:13 Ekom asked Jan 11, 2017 at 0:56 EkomEkom 6293 gold badges11 silver badges24 bronze badges 8
  • Maybe try .toLocaleString() or something like new Date(Date.UTC(year, month, day, hour, minute, second)) – aggaton Commented Jan 11, 2017 at 1:14
  • @aggaton Thanks for your response. I used new Date().toLocaleString(); and I got 1/10/2017, 8:20:30 PM not exactly what I'm looking for. I will like the time in TZ format. However new Date(Date.UTC(year, month, day, hour, minute, second)) gave an error of year is not defined – Ekom Commented Jan 11, 2017 at 1:23
  • Um, the time of your post is 2017-01-11 00:56:01Z. No idea where you lost one and a half hours? – Bergi Commented Jan 11, 2017 at 2:24
  • 2 Obviously I spent the time trying to make sure the post was well explanatory while searching through suggested answers to my question. Do you have a solution or you're trying to be funny? – Ekom Commented Jan 11, 2017 at 2:27
  • It seems your times are correct, you just don't realize it's in UTC (that's what the Z 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 same Date object; and no, this does not affect how they are stored in MongoDB. – Bergi Commented Jan 11, 2017 at 2:28
 |  Show 3 more comments

2 Answers 2

Reset to default 7

Using moment.js is the easiest way to accomplish what you are asking.

moment().format() // "2017-01-11T13:56:15-05:00"

The output is a string in ISO-8601 format, with time zone offset in effect in your local time zone.

You could do this yourself with a lot of code that reads the various properties of the Date object, building a string from those values. But it is not built-in to the Date object in this way.

Also, note any time you try to adjust a Date object by a time zone offset, you are simply picking a different point in time. You're not actually changing the behavior of the time zone being used by the Date object.

If you don't want to use any exteral JS file, You can simply use following code to get current timezone.

new Date().toString();
发布评论

评论列表(0)

  1. 暂无评论