在JQuery中将String转成Data类型,首先要将字符串格式化,将其化为“2017/8/9”或者“8/9/2017”,其结果是一样的,前者是我们常用的格式:年月日,后者是美国人常用的格式:月日年。
还有我们常用的格式“2017-8-9”中间以‘ - ’连接,这种格式为 ISO 8601 格式,浏览器有的支持有的不支持,具体如下:
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
ISO8601 format | (Yes) | 4.0 (2.0) | 9 | (Yes) | (Yes) |
所以还是建议使用“2017/8/9”这种格式。
var data = "20170809"
var data1 = data.substr(0,4)+"/"+data.substr(4,2)+"/"+data.substr(6,2)
// 此时data1 = 2017/08/09
var da_1 = Date.parse(data1); //此时 da_1 = 1502121600000 毫秒数
var da_2 = new Data(Date.parse(data1)) //da_2 = Tue Aug 08 2017 00:00:00 GMT+0800 (中国标准时间)
一般 da_1 的形式就可以了,然后可以和当前时间或者其他时间相减得到毫秒数,然后再除以1000除以3600除以24 得到二者之间的天数。