Json Stringify 日期与 javascript 日期相比产生了错误的日期

Json Stringify date produces a wrong date compared to javascript date

当我创建一个 javascript 日期然后 stringify 它并将其发送到服务器时,我得到两个不同的日期。 stringified 日期总是晚一天。

所以目前我将我的 javascript 日期增加 1 天,以便我在服务器上收到相同的日期。

我当前的代码:

var dt = $(.datepicker).datepicker('getDate');//Fri Aug 26 2016 00:00:00 GMT+0200 (South Africa Standard Time)
var result = Json.stringify(dt); //"2016-08-25T22:00:00.000Z"

这是正确的方法还是我遗漏了什么?

使用这个

var result = Json.stringify(dt.toISOString());

这是由于 Date 中的时区组件造成的。我所做的工作是:

var date = $(.datepicker).datepicker('getDate');
var utcDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes()))
var result = Json.stringify(utcDate);

删除时区组件。

您似乎不明白您的两个日期时间实际上是相同的并且是正确的。您还没有解释为什么您认为需要手动更改发送到服务器的那个。这是一个示例,证明它们实际上是相同的,只是以不同的格式显示在不同的 timezones.

// Values from the local datetime string
var local = {
  year: 2016,
  month: 7,
  day: 26,
  hours: 0,
  minutes: 0,
  seconds: 0,
  milliseconds: 0
};

// Values from the UTC ISO 8601 datetime string
var utc = {
  year: 2016,
  month: 7,
  day: 25,
  hours: 22,
  minutes: 0,
  seconds: 0,
  milliseconds: 0
};

// Create Date object as local
var date1 = new Date(
  local.year,
  local.month,
  local.day,
  local.hours,
  local.minutes,
  local.seconds,
  local.milliseconds
);

// Create Date object as local from UTC
var date2 = new Date(Date.UTC(
  utc.year,
  utc.month,
  utc.day,
  utc.hours,
  utc.minutes,
  utc.seconds,
  utc.milliseconds
));

var pre = document.getElementById('out');
// Display Date1 as local
pre.appendChild(document.createTextNode(date1.toString() + '\n'));
// Display Date2 as local
pre.appendChild(document.createTextNode(date2.toString() + '\n'));
// Display Date2 as UTC
pre.appendChild(document.createTextNode(date2.toUTCString() + '\n'));
// Test if Date1 and Date2 display the same datetime
pre.appendChild(document.createTextNode(
  'Date1 === Date2: ' + (date1.getTime() === date2.getTime())
));
<pre id="out"></pre>

JSON 将 Date 对象转换为 ISO 8601(根据规范),但让我们看看如果您使用您选择的解决方案会发生什么。

// Values from the local datetime string
var local = {
  year: 2016,
  month: 7,
  day: 26,
  hours: 0,
  minutes: 0,
  seconds: 0,
  milliseconds: 0
};

// Create Date object as local
var date = new Date(
  local.year,
  local.month,
  local.day,
  local.hours,
  local.minutes,
  local.seconds,
  local.milliseconds
);

// Your solution
var utcDate = new Date(Date.UTC(
  date.getFullYear(),
  date.getMonth(),
  date.getDate(),
  date.getHours(),
  date.getMinutes()));

var pre = document.getElementById('out');
// Display Date as local format
pre.appendChild(document.createTextNode(date.toString() + '\n'));
// Display utcDate as local format
pre.appendChild(document.createTextNode(utcDate.toString() + '\n'));
// Test if Date and utcDate display the same datetime
pre.appendChild(document.createTextNode(
  'Date1 === Date2: ' + (date.getTime() === utcDate.getTime())
));
<pre id="out"></pre>

您最终得到 2 个不再相同的日期。不喜欢ISO 8601 for transmission and storage of datetimes? Well the alternative would be to use the number of milliseconds UTC since the epoch (getTime). You can't make JSON do this conversion instead of ISO 8601, not even using a replacer function. So any conversion would be necessary before using JSON.stringify。所以你真的需要解释你正在努力实现的目标以及为什么你认为你现在拥有的是不正确的。