JavaScript中特定时区的新Date() [英] new Date() for a specific timezone in JavaScript

查看:161
本文介绍了JavaScript中特定时区的新Date()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目上,我只是好奇是否可以在JavaScript中为特定时区调用new Date()。当我说:

I am working on a project and I was just curious if it was possible in JavaScript to call new Date() for a specific timezone. When I say:

var test = new Date();

我调试了,发现var test =

I debug and I find that the var test =

Date {Fri Nov 07 2014 11:59:41 GMT-0500 (Eastern Standard Time)}

这很有意义,因为我在东部时区,这里是11:59。

which makes sense because I am in the Eastern Time Zone and it is 11:59 here.

我的问题是: 是否可以获取指定时区的Date对象,而不仅仅是您自己的本地系统时间?

我有预订我正在将开始会议时输入的时间与实际时间进行比较的表格是现在要确保用户没有输入过去的时间或当前时间的2小时内的时间(不同的错误验证消息)

I have a reservation form where I am comparing the time entered for the start of a conference with the actual time is it now to make sure that the user doesn't enter a past time, or a time within 2 hours from the current time(different error validation messages get thrown up for each instance).

我唯一的问题是填写表格的人可能在东部,中部,山区或太平洋。因此,如果位于东部时间的某人填写了一份会议表格,其中包含客户在山区时间的开始时间,那么当我尝试比较时间时,new Date()仅在需要时提供我的本地时间将输入的时间与山区时间进行比较以进行检查和确认。

My only problem is that the person who fills out the form can be either in Eastern, Central, Mountain, or Pacific. So if a person located in Eastern Time fills out a form for a conference with a start time for a customer in Mountain time, when I try to compare the times, new Date() only gives me my local time, when I would want it to compare the time entered to Mountain time for my checks and validation.

这有点令人困惑,但我只想向您介绍此问题的申请背景。如果有人需要进一步澄清,请随时提出。

Its sort of confusing but I just wanted to give you a little background on the application for this issue. If anyone needs further clarification please feel free to ask. Any help is greatly appreciated.

谢谢!

-戴夫

推荐答案

结帐 moment-timezone.js 在JavaScript中使用日期和时区要容易得多。

Checkout moment-timezone.js which makes working with Dates and timezones a lot easier in javascript.

首页上的示例显示了其工作原理:

The example on the frontpage shows how it works:

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london     = newYork.clone().tz("Europe/London");

newYork.format();    // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format();     // 2014-06-01T17:00:00+01:00

这篇关于JavaScript中特定时区的新Date()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆