我有一个网页,有三个下拉菜单,分别是日、月和年。如果我使用JavaScript Date构造函数接受数字,那么我得到一个Date对象为我的当前时区:

new Date(xiYear, xiMonth, xiDate)

给出正确的日期,但由于夏令时,它认为日期是GMT+01:00。

这里的问题是,然后我将这个日期传递给一个Ajax方法,当日期在服务器上被反序列化时,它已经转换为GMT,因此失去了一个小时,这将一天往回移动了一个小时。 现在我可以将日、月和年分别传递到Ajax方法中,但似乎应该有更好的方法。

接受的答案为我指明了正确的方向,但是仅仅使用setUTCHours()本身就改变了:

Apr 5th 00:00 GMT+01:00 

to

Apr 4th 23:00 GMT+01:00

然后,我还必须设置UTC日期、月份和年份

Apr 5th 01:00 GMT+01:00

这正是我想要的


当前回答

我所看到的最佳解决方案来自

http://www.codingforums.com/archive/index.php/t-19663.html

打印时间命令功能

<script language="javascript" type="text/javascript">
//borrowed from echoecho
//http://www.echoecho.com/ubb/viewthread.php?tid=2362&pid=10482&#pid10482
workDate = new Date()
UTCDate = new Date()
UTCDate.setTime(workDate.getTime()+workDate.getTimezoneOffset()*60000)

function printTime(offset) {
    offset++;
    tempDate = new Date()
    tempDate.setTime(UTCDate.getTime()+3600000*(offset))
    timeValue = ((tempDate.getHours()<10) ? ("0"+tempDate.getHours()) : (""+tempDate.getHours()))
    timeValue += ((tempDate.getMinutes()<10) ? ("0"+tempDate.getMinutes()) : tempDate.getMinutes())
    timeValue += " hrs."
    return timeValue
    }
    var now = new Date()
    var seed = now.getTime() % 0xfffffff
    var same = rand(12)
</script>

Banff, Canada:
<script language="JavaScript">document.write(printTime("-7"))</script>

完整代码示例

<html>

<head>
<script language="javascript" type="text/javascript">
//borrowed from echoecho
//http://www.echoecho.com/ubb/viewthread.php?tid=2362&pid=10482&#pid10482
workDate = new Date()
UTCDate = new Date()
UTCDate.setTime(workDate.getTime()+workDate.getTimezoneOffset()*60000)

function printTime(offset) {
offset++;
tempDate = new Date()
tempDate.setTime(UTCDate.getTime()+3600000*(offset))
timeValue = ((tempDate.getHours()<10) ? ("0"+tempDate.getHours()) : (""+tempDate.getHours()))
timeValue += ((tempDate.getMinutes()<10) ? ("0"+tempDate.getMinutes()) : tempDate.getMinutes())
timeValue += " hrs."
return timeValue
}
var now = new Date()
var seed = now.getTime() % 0xfffffff
var same = rand(12)
</script>

</head>

<body>
Banff, Canada:
<script language="JavaScript">document.write(printTime("-7"))</script>
<br>
Michigan:
<script language="JavaScript">document.write(printTime("-5"))</script>
<br>
Greenwich, England(UTC):
<script language="JavaScript">document.write(printTime("-0"))</script>
<br>
Tokyo, Japan:
<script language="JavaScript">document.write(printTime("+9"))</script>
<br>
Berlin, Germany:
<script language="JavaScript">document.write(printTime("+1"))</script>

</body>
</html>

其他回答

const date = new Date("2020-12-16 17:45:00 UTC");

工作很好。

使用. settutchours()可以实际设置utc时间的日期,这将允许您在整个系统中使用utc时间。

但是不能在构造函数中使用UTC设置它,除非指定日期字符串。

使用新的日期(日期。UTC(年,月,日,小时,分,秒)),您可以从特定的UTC时间创建一个日期对象。

d = new Date();
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
nd = new Date(utc + (3600000*offset));

offset value base on which location time zone you would like to set 
For India offset value +5.5,
New York offset value -4,
London offset value +1

对于所有位置偏移量Wiki UTC时间偏移量列表

GMT -03:00示例

new Date(new Date()-3600*1000*3).toISOString();  // 2020-02-27T15:03:26.261Z

甚至

now  = new Date().getTime()-3600*1000*3; // 1582818380528
data = new Date(now).toISOString();      // 2020-02-27T15:03:26.261Z

下面是一个函数,你可以用它来实现一个date对象:

const getUTCDate = (date) => {
    const d = new Date(date);
    const utcDate = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
    return utcDate;
}

const string = '1930-08-12T00:00:00.000000Z';

const utcDate = getUTCDate(string);

// returns Tue Aug 12 1930 00:00:00 GMT-0800 (Pacific Daylight Time)