我正在开发一个针对移动设备的具有离线功能的 Javascript 网站。一种这样的移动设备是 iPhone。
我正在尝试从我们的 REST API(JSON 对象的成员)解析日期。我正在使用
Date.parse("2010-03-15 10:30:00");
这适用于 Android 设备,但在 iPhone 上它只是给出一个无效的日期。
我如何需要格式化我的日期字符串以便它可以被 iPhone 解析?
我正在开发一个针对移动设备的具有离线功能的 Javascript 网站。一种这样的移动设备是 iPhone。
我正在尝试从我们的 REST API(JSON 对象的成员)解析日期。我正在使用
Date.parse("2010-03-15 10:30:00");
这适用于 Android 设备,但在 iPhone 上它只是给出一个无效的日期。
我如何需要格式化我的日期字符串以便它可以被 iPhone 解析?
并非所有浏览器都支持相同的日期格式。最好的方法是在分隔符(和)上拆分字符串-
,并将每个结果数组项传递给构造函数:
:
Date
var arr = "2010-03-15 10:30:00".split(/[- :]/),
date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]);
console.log(date);
//-> Mon Mar 15 2010 10:30:00 GMT+0000 (GMT Standard Time)
这在所有浏览器中都是一样的。
For UTC/GMT time, you can try:
var arr = "2014-10-27T16:05:44+0000".split(/[\-\+ :T]/);
var date = new Date();
date.setUTCFullYear(arr[0]);
date.setUTCMonth(arr[1] - 1);
date.setUTCDate(arr[2]);
date.setUTCHours(arr[3]);
date.setUTCMinutes(arr[4]);
date.setUTCSeconds(arr[5]);
The date object will display in the proper local timezone when used.
如果您坚持使用ISO 8601格式,您可能会有更好的运气:
Date.parse("2010-03-15T10:30:00");
// e.g.
var d = new Date( Date.parse("2010-03-15T10:30:00") );
console.log( d.toString() ); //Mon Mar 15 2010 10:30:00 GMT+0000 (BST)
2019-07-29 更新:不删除以下现有内容,但我强烈建议您不要使用此方法。我从错误中吸取了教训。不要覆盖 JavaScript 的现有方法。对您的代码的可移植性和性能不利。如果您无法获得 ISO 8601(这是 JS 和大多数 API 的标准做法),那么请改为更改该系统。或者,编写您自己的函数,始终必须调用该函数来生成Date
对象。
如果您可以更正 REST API 以发送正确的 ISO 8601 日期时间,则处理此问题的正确方法是使用允许所有浏览器处理 ISO 8601 日期的正则表达式 shim。当然它可能会更慢,但使用 Javascript 处理日期就像试图让一只猫进入一桶水。
Keep in mind the following method overrides the original Date.parse method. This could be problematic in larger projects or with many developers with different expectations. Use only if you're aware of what you're doing.
/**
* Date.parse with progressive enhancement for ISO 8601 <https://github.com/csnover/js-iso8601>
* © 2011 Colin Snover <http://zetafleet.com>
* Released under MIT license.
*/
(function (Date, undefined) {
var origParse = Date.parse, numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ];
Date.parse = function (date) {
var timestamp, struct, minutesOffset = 0;
// ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
// before falling back to any implementation-specific date parsing, so that’s what we do, even if native
// implementations could be faster
// 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm
if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) {
// avoid NaN timestamps caused by “undefined” values being passed to Date.UTC
for (var i = 0, k; (k = numericKeys[i]); ++i) {
struct[k] = +struct[k] || 0;
}
// allow undefined days and months
struct[2] = (+struct[2] || 1) - 1;
struct[3] = +struct[3] || 1;
if (struct[8] !== 'Z' && struct[9] !== undefined) {
minutesOffset = struct[10] * 60 + struct[11];
if (struct[9] === '+') {
minutesOffset = 0 - minutesOffset;
}
}
timestamp = Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]);
}
else {
timestamp = origParse ? origParse(date) : NaN;
}
return timestamp;
};
}(Date));
There is bug in the
var date = new Date();
date.setUTCFullYear(arr[0]);
date.setUTCMonth(arr[1] - 1);
date.setUTCDate(arr[2]);
date.setUTCHours(arr[3]);
date.setUTCMinutes(arr[4]);
date.setUTCSeconds(arr[5]);
approach. If Date() is the end of month and the month set has less days then the result is one month out.
Wrapping Up with the string will do the magic. Since the safari has some unique way of parsing and formatting date
Date.parse(String("2010-03-15 10:30:00"));
Still facing issue use the Moment.js
Happy to help. :-)