将Javascript Date对象转换为PST时区 [英] Convert Javascript Date object to PST time zone

查看:504
本文介绍了将Javascript Date对象转换为PST时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从日历中选择未来的日期,假设我选择的日期是 10/14/2014 ,现在我想要的是将日期发送到时间到服务器,所以在服务器端总是在PST时区达到上午6点,日期的格式应该是UTC。

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.

我在做什么是

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

我的理解是PST时区是-8:00,所以我已经添加14小时到UTC时间,以便时间成为PST上午6:00

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

我遇到的问题是,不要让我添加14小时,因为对象已经已转换为字符串。

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.

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

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

如果我写

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.

任何帮助都不胜感激。

推荐答案

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

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

这是一个替代方案:

使用:Date.UTC(year,月,日,小时,分,秒,毫秒)

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

示例:
2013年1月1日6AM PST

Example: For 1 Jan 2013 6AM PST


var date = new Date(Date.UTC(2013,0,1,14,0,0))

console.log(date.toUTCString() );

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

打印:星期二,2013年1月1日14:00:00 GMT

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

这篇关于将Javascript Date对象转换为PST时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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