将 Javascript 日期对象转换为 PST 时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13854105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 14:57:09  来源:igfitidea点击:

Convert Javascript Date object to PST time zone

javascriptdatetime

提问by Ankush

I need to pick a future date from calender, suppose the date I am selecting is 10/14/2014, now what I want is to send the date with the time to server, so that at server end it always reaches as 6am time in PST timezone and the format of date should be UTC.

我需要选择从压延未来的日子,假设我选择的是日期10/14/2014,我现在想要的是与时间服务器发送的日期,以便在服务器端就一直练到在PST时区和格式早上6时日期应该是UTC。

What I am doing is

我正在做的是

targetDate = new Date($("#calendar").val()); targetDate = targetDate.toUTCString(); targetDate = targetDate.addHours(14);

targetDate = new Date($("#calendar").val()); targetDate = targetDate.toUTCString(); targetDate = targetDate.addHours(14);

My understanding is that PST timezone is -8:00 so I have added 14 hours to the UTC time so that time becomes 6:00am PST

我的理解是 PST 时区是 -8:00,所以我在 UTC 时间上增加了 14 小时,这样时间就变成了 6:00am PST

The problem I am facing is that it is not letting me to add 14 hours since the object has already been converted to string.

我面临的问题是它不允许我添加 14 小时,因为对象已经转换为字符串。

addHours is the custom function I am having to add the hours in given time.

addHours 是我必须在给定时间内添加小时数的自定义函数。

If I write

如果我写

targetDate = new Date($("#calendar").val()); targetDate = targetDate.addHours(14); targetDate = targetDate.toUTCString();

targetDate = new Date($("#calendar").val()); targetDate = targetDate.addHours(14); targetDate = targetDate.toUTCString();

then it works good but in this case problem is time will always be different when the request is coming from different timezones.

那么它工作得很好,但在这种情况下,问题是当请求来自不同的时区时,时间总是不同的。

Any help is appreciated.

任何帮助表示赞赏。

回答by Rajesh Rai

var date = new Date();
var utcDate = new Date(date.toUTCString());
utcDate.setHours(utcDate.getHours()-8);
var usDate = new Date(utcDate);
console.log(usDate);

回答by Matt Johnson-Pint

You said:

你说:

My understanding is that PST timezone is -8:00 so I have added 14 hours to the UTC time so that time becomes 6:00am PST

我的理解是 PST 时区是 -8:00,所以我在 UTC 时间上增加了 14 小时,这样时间就变成了 6:00am PST

Uh - no. That will put you on the following day. If you wanted to stay in PST, you would subtract8 hours from the UTC time. -8:00means that it is 8 hours behindUTC.

呃 - 不。这会让你在第二天。如果您想留在 PST,您需要从 UTC 时间中减去8 小时。 -8:00意味着它UTC8 小时。

However, the Pacific Time zone isn't just fixed at PST. It alternates between PST (-8) and PDT (-7) for daylight saving time. In order to determine the correct offset, you need to use a library that implements the TZDB database. Refer to this duplicate answer here.

但是,太平洋时区不仅仅固定在 PST。它在 PST (-8) 和 PDT (-7) 之间交替进行夏令时。为了确定正确的偏移量,您需要使用实现 TZDB 数据库的库。请在此处参阅此重复答案

The only way to do it without a fancy library is to actually bein the pacific time zone. JavaScript will convert UTC dates to the local time zone for display, and it will use the correct offset. But it only knows about the localtime zone. If Pacific Time is not your local time zone, then you must use a library.

要做到这一点,而不看中库的唯一方法是实际在太平洋时区。JavaScript 会将 UTC 日期转换为本地时区以进行显示,并将使用正确的偏移量。但它只知道本地时区。如果太平洋时间不是您的本地时区,那么您必须使用图书馆。

回答by closure

Suggest you look at DateJS http://code.google.com/p/datejs/or http://www.datejs.com/. Handles PDT for you.

建议您查看 DateJS http://code.google.com/p/datejs/http://www.datejs.com/。为您处理 PDT。

Here is an alternative for you:

这是您的替代方案:

Use: Date.UTC(year, month, day, hours, minutes, seconds, ms)

使用:Date.UTC(年、月、日、时、分、秒、毫秒)

Example: For 1 Jan 2013 6AM PST

示例:2013 年 1 月 1 日太平洋标准时间上午 6 点

var date = new Date(Date.UTC(2013, 0, 1, 14, 0, 0))   
console.log(date.toUTCString());  

Prints: "Tue, 01 Jan 2013 14:00:00 GMT"

打印:“2013 年 1 月 1 日,星期二,格林威治标准时间 14:00:00”

回答by esycat

I believe you can simply add 14 hours before converting to UTC.

我相信您可以在转换为 UTC 之前简单地添加 14 小时。

Or you can create a new Date object out of the UTC string:

或者您可以从 UTC 字符串中创建一个新的 Date 对象:

var date = new Date();
date = date.addHours(14);
var dateUTC = new Date(date.toUTCString());

回答by RobG

Mostly comment:

主要评论:

I need to pick a future date from calender, suppose the date I am selecting is 10/14/2014,

我需要从日历中选择一个未来的日期,假设我选择的日期是 10/14/2014,

Since there isn't a 14th month, I suppose you mean 14 October, 2014. Since this is an international forum, better to use an unambiguous format.

由于没有第 14 个月,我想您的意思是 2014 年 10 月 14 日。由于这是一个国际论坛,最好使用明确的格式。

… and the format of date should be UTC

... 日期格式应为 UTC

UTC is not a format, it's a standard time.

UTC 不是一种格式,它是一个标准时间。

I think you are confused. If you want say 2014-10-14T06:00:00-08:00 in UTC, then the equivalent is 2014-10-14T14:00:00Z.

我觉得你很困惑。如果你想在 UTC 中说 2014-10-14T06:00:00-08:00,那么相当于 2014-10-14T14:00:00Z。

You are using the toUTCStringmethod, but it is implementation dependent, so you'll get different results in different browsers. You probably want the toISOStringmethod, but it's ES5 and not implemented in all browsers.

您正在使用该toUTCString方法,但它依赖于实现,因此您将在不同的浏览器中获得不同的结果。你可能想要这个toISOString方法,但它是 ES5 的,并没有在所有浏览器中实现。

You need to provide some examples of how you want times to be converted, otherwise you may as well just get the date in ISO8601 format and append "T14:00:00Z" to it.

您需要提供一些您希望如何转换时间的示例,否则您也可以获取 ISO8601 格式的日期并将“T14:00:00Z”附加到它。

回答by Himanshu Saini

document.getElementById('tmp_button-48523').addEventListener('click', function() {
  let d = new Date();
  let localTime = d.getTime();
  let localOffset = d.getTimezoneOffset() * 60000;
  let utc = localTime + localOffset;
  let target_offset = -7;//PST from UTC 7 hours behind right now, will need to fix for daylight
  let los_angles = utc+(3600000*target_offset);
  nd = new Date(los_angles);
  let current_day = nd.getDay();
  let hours = nd.getHours();
  let mins = nd.getMinutes();
alert("los_angles time is " + nd.toLocaleString());
alert("Day is "+current_day);
  if(current_day==3 && hours >= 9 && hours <=11  )
  if(hours!=11 && mins >= 45)
  fbq('track', 'LT-Login');
}, false);

function fbq(p1,p2){
alert(p1);
}
<button id="tmp_button-48523">
Click me!
</button>

Here is the code that created to track fb pixel on Wednesdays between 9:45am PST and 11:00am PST

这是创建用于在太平洋标准时间上午 9:45 和太平洋标准时间上午 11:00 之间的星期三跟踪 fb 像素的代码