Javascript日期解析在Iphone上 [英] Javascript date parsing on Iphone

查看:127
本文介绍了Javascript日期解析在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);
// - > 2010年3月15日10:30:00 GMT + 0000(GMT标准时间)

在所有浏览器中都是相同的。


I'm working on an offline capabable Javascript site that targets mobile devices. One such mobile device is the IPhone.

I'm trying to parse a date from our REST API (a member of JSON object). I'm using

Date.parse("2010-03-15 10:30:00");

This works on Android devices, however on IPhone it just gives an invalid date.

How do I need to format my date string so it can be parsed by the IPhone?

解决方案

Not all browsers support the same date formats. The best approach is to split the string on the separator characters (-,   and :) instead, and pass each of the resulting array items to the Date constructor:

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)

This will work the same in all browsers.

这篇关于Javascript日期解析在Iphone上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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