假设您网站的用户输入了一个日期范围。

2009-1-1 to 2009-1-3

您需要将此日期发送到服务器进行某些处理,但服务器要求所有日期和时间均为UTC。

现在假设用户在阿拉斯加。由于它们所处的时区与UTC完全不同,因此需要将日期范围转换为如下所示:

2009-1-1T8:00:00 to 2009-1-4T7:59:59

使用JavaScriptDate对象,如何将第一个“本地化”日期范围转换为服务器能够理解的内容?


当前回答

我的解决方案使日期保持不变,无论客户端上设置了什么时区。也许有人会发现它很有用。

我的用例:

我正在创建一个todo应用程序,您可以在其中设置任务日期。无论您在哪个时区,此日期都应保持不变。

实例你想在6月25日早上8点给你的朋友打电话。

您在中国时,在5天前(6月20日)创建此任务。

然后,在同一天,你飞往纽约几天。

然后在6月25日,当你还在纽约时,你在早上7:30醒来(这意味着你应该在30分钟内收到任务通知(即使在中国,你创建任务时已经是下午1:30)

因此,任务是忽略时区。这意味着“我想在早上8点在任何时区做这件事”。

我所做的是让我们说“我假设你总是在伦敦时区-UTC”。

这意味着-当用户在她/他的时区中选择某个日期时-我将此日期转换为UTC中的相同日期。也就是说,你在中国选择上午8点,但我将其转换为UTC的上午8点。

然后-下次你打开应用程序时-我读取以UTC保存的日期,并将其转换为当前时区中的相同日期-例如,我将UTC中的上午8点转换为纽约时区中的早上8点。

这个解决方案意味着,日期可能意味着其他东西,这取决于你在设置日期时的位置和阅读日期的位置,但它保持不变,“感觉”你总是在同一时区。

让我们编写一些代码:

首先,我们有两个主要函数用于从UTC转换为UTC,忽略时区:

export function convertLocalDateToUTCIgnoringTimezone(date: Date) {
  const timestamp = Date.UTC(
    date.getFullYear(),
    date.getMonth(),
    date.getDate(),
    date.getHours(),
    date.getMinutes(),
    date.getSeconds(),
    date.getMilliseconds(),
  );

  return new Date(timestamp);
}

export function convertUTCToLocalDateIgnoringTimezone(utcDate: Date) {
  return new Date(
    utcDate.getUTCFullYear(),
    utcDate.getUTCMonth(),
    utcDate.getUTCDate(),
    utcDate.getUTCHours(),
    utcDate.getUTCMinutes(),
    utcDate.getUTCSeconds(),
    utcDate.getUTCMilliseconds(),
  );
}

然后,我保存/读取此日期,如:

function saveTaskDate(localDate: Date) {
  // I convert your local calendar date so it looks like you've picked it being in UTC somewhere around London
  const utcDate = convertLocalDateToUTCIgnoringTimezone(localDate);
  api.saveTaskDate(utcDate);
}

function readTaskDate(taskUtcDate: Date) {
  // I convert this UTC date to 'look in your local timezone' as if you were now in UTC somewhere around london
  const localDateWithSameDayAsUTC = convertUTCToLocalDateIgnoringTimezone(taskUtcDate);

  // this date will have the same calendar day as the one you've picked previously
  // no matter where you were saving it and where you are now
}

其他回答

var myDate = new Date(); // Set this to your date in whichever timezone.
var utcDate = myDate.toUTCString();

简单又愚蠢

var date=新日期();var now_utc=日期utc(Date.getUTCFullYear(),Date.getUTCMonth(),date.getUTCDate(),date.getUTPours(),date.getUTCMinutes(),date.getUTCSeconds());console.log(新日期(now_utc));console.log(date.toISOString());

所以这是我必须要做的,因为我仍然希望JavaScript日期对象作为日期进行操作,而不幸的是,这些答案中的很多都需要您转到字符串。

//First i had a string called stringDateVar that i needed to convert to Date
var newDate = new Date(stringDateVar)

//output: 2019-01-07T04:00:00.000Z
//I needed it 2019-01-07T00:00:00.000Z because i had other logic that was dependent on that 

var correctDate = new Date(newDate.setUTCHours(0))

//This will output 2019-01-07T00:00:00.000Z on everything which allows scalability 

这是我的方法:

var now = new Date();
var utc = new Date(now.getTime() + now.getTimezoneOffset() * 60000);

生成的utc对象实际上不是utc日期,而是转换为与utc时间匹配的本地日期(请参见注释)。然而,在实践中,它确实起到了作用。


更新:当调用UTC.toString()、UTC.toLocaleString()等时,这个答案是获取UTC日期的一种快速而肮脏的方法。不过,有更好的解决方案,尤其是现在使用现代浏览器的情况下,我应该改进一下答案。基本上,.toISOString()(IE 9+)是您想要使用的。

甚至更简单

myvar.setTime(myvar.getTime() + myvar.getTimezoneOffset() * 60000);