将 Javascript 日期对象设置为午夜而不基于用户计算机日期
无论用户的计算机日期如何,我都需要将日期和时间设置为午夜 12 点。我正在创建一个国际会议策划程序,以抵消时区以召开会议。
我已经可以工作了,但我现在需要对时区的差异进行编码。如果我使用新日期,它会根据用户的计算机为我提供时间。例如,我的是美国东部。如果我尝试在 2011 年 11 月 6 日进行时区转换,Javascript/计算机将在凌晨 2 点计算我的时区转换。我不想要这个。
我的真正目标是将其设置为会议举办地所在时区的午夜 12 点(假设是阿富汗),然后从那里开始计算。
那么:
如何设置午夜 12 点而不是用户的计算机时间?
我可以将午夜 12 点设置为特定时区,而不依赖于用户的计算机时间吗?
我必须使用 Javascript 来完成此操作,因为不涉及服务器代码。
谢谢
I need to set a date and time to 12 midnight regardless of the user's computer date. I am creating an international meeting planner to offset time zones to get the meeting.
I have it working but I need to code now the differences in time zones. If I use new Date, it gives me the time based on the user's computer. For example, mine is Eastern US. If I try to do the time zone shift on November 6, 2011, Javascript/computer will calculate MY time zone shift at 2am. I do not want this.
My real goal is to set it to 12 midnight in the timezone that is where the meeting will be hosted from (let's say Afghanistan) and then calculate from there.
So:
How do I set 12 midnight without being being the user's computer time?
Can I set 12 midnight to a specific time zone, without being dependent on user's computer time?
I have to do this with Javascript as there is no server code involved.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜想是这样的:
new Date(Date.UTC(年、月、日、小时、分钟、秒))
w3schools 参考
Something like this I would guess:
new Date(Date.UTC(year, month, day, hour, minute, second))
w3schools Reference
创建 GMT 午夜:
创建另一个时区的午夜:
日期对象以用户的本地时区表示,但它们仍然代表另一个时区的午夜。
如果您想表达另一个时区的日期,则需要将日期偏移到该时区 (
setUTCHours()
),然后使用各种 getUTC* 方法(例如getUTCHours( )
) 构造您自己的字符串。To create midnight in GMT:
To create midnight in another time zone:
Date objects are expressed in the local time zone of the user, but they are still representative of midnight in another time zone.
If you want to express a date in another time zone, you'll need to offset the date to that timezone (
setUTCHours()
) and then use the various getUTC* methods (e.g.getUTCHours()
) to construct your own string.