javascript 时间处理问题

来源:互联网 发布:php文件管理系统 编辑:程序博客网 时间:2024/06/08 02:38
/// 使用本机获取时间var a = new Date();/// 通过简易的时间串初始化var b = new Date('2015-10-29');/// 使用带有时间的串初始化var c = new Date('2015-10-29 00:00:00');/// 使用ISO标准串格式化var d = new Date('2015-10-29T00:00:00');/// 使用具体参数初始化var e = new Date(2015,9,29,0,0,0);/// 使用默认时间参数初始化var f = new Date(2015,9,29);console.log('a=', a.getTime(), a.toISOString(), a.toLocaleString(), a.toTimeString());console.log('b=', b.getTime(), b.toISOString(), b.toLocaleString(), b.toTimeString());console.log('c=', c.getTime(), c.toISOString(), c.toLocaleString(), c.toTimeString());console.log('d=', d.getTime(), d.toISOString(), d.toLocaleString(), d.toTimeString());console.log('e=', e.getTime(), e.toISOString(), e.toLocaleString(), e.toTimeString());console.log('f=', f.getTime(), f.toISOString(), f.toLocaleString(), f.toTimeString());

 

输出结果为:

a= 1446114992991 2015-10-29T10:36:32.991Z Thu Oct 29 2015 18:36:32 GMT+0800 (中国标准时间) 18:36:32 GMT+0800 (中国标准时间)
b= 1446076800000 2015-10-29T00:00:00.000Z Thu Oct 29 2015 08:00:00 GMT+0800 (中国标准时间) 08:00:00 GMT+0800 (中国标准时间)
c= 1446048000000 2015-10-28T16:00:00.000Z Thu Oct 29 2015 00:00:00 GMT+0800 (中国标准时间) 00:00:00 GMT+0800 (中国标准时间)
d= 1446076800000 2015-10-29T00:00:00.000Z Thu Oct 29 2015 08:00:00 GMT+0800 (中国标准时间) 08:00:00 GMT+0800 (中国标准时间)
e= 1446048000000 2015-10-28T16:00:00.000Z Thu Oct 29 2015 00:00:00 GMT+0800 (中国标准时间) 00:00:00 GMT+0800 (中国标准时间)
f= 1446048000000 2015-10-28T16:00:00.000Z Thu Oct 29 2015 00:00:00 GMT+0800 (中国标准时间) 00:00:00 GMT+0800 (中国标准时间)

 

可以得出的结论为:

a: 获得的是现在本初子午线的1970年到现在的毫秒数

b:获得的是本初子午线2015-10-29 0点的毫秒数 但是Date().toString() 打印的时候会格式化成带有时区(东八区)的8点 这个方式等同于 a方式

c:获得带有时区(机器当前时区)对应本初子午线-8的时间戳 注意这种方式和 a b存在8小时时差

d:获取iso标准的本初子午线时间的时间戳,等同于 a, b 不同与c

e:等同于c,和本初子午线差距8小时.

f:等同于c 和本初子午线差距8小时

 

最总结论:

在编码的时候如果需要根据时间戳做时间对比,按照以上规范区分使用

a b d三种方式获得的是不带时区的时间戳

c e f三种方式是根据本机所在时区对应的时间戳

0 0
原创粉丝点击