JS new Date 处理时间
JS Date 处理时间有个坑:
举例:
1、new Date("2016-10-26 16:17:18.667") 结果为:Wed Oct 26 2016 16:17:18 GMT+0800 (中国标准时间)
2、new Date("2016-10-26T16:17:18.667") 结果为: Thu Oct 27 2016 00:17:18 GMT+0800 (中国标准时间)
注意1和2的差别: 中间T, 有T 过16点,就会自动增加一天。
两者转换:
var ss = new Date("2016-10-26 16:17:18.667")
ss.toISOString() 结果:"2016-10-26T08:17:18.667Z" /// ISO : 标准时间 UTC 时间
T: 的意义是什么呢? 待学
处理方法:
JS 格式化方法:(从网上找的别人的)
Date.prototype.Format = function (fmt) { //author: meizz 扩展函数
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
因为我的需求 只是显示日期,所以只取日期部分进行new Date():
var DateString = "2016-10-26T16:17:18.667";
var Date1 = new Date(DateString.replace(/-/g, "/").substring(0, 10)).Format("MM/dd/yyyy")
PS: 还有一点, 不是所有的浏览器JS都支持2016-10-26 这种带”-“的时间写法, 所以不建议使用这样的日期格式进行new Date(), 不同浏览器会出问题。
基于'/'格式的日期字符串,才是被各个浏览器所广泛支持的, 所以将字符串日期 DateString.replace(/-/g, "/") 处理成"/"类型。
常识:
- CST:China Standard Time ,等同于chrome浏览器(中国标准时间)。
- GMT: Greenwich Mean Time,格林尼治标准时间
- UTC:Coordinated Universal Time,协调世界时,又称世界标准时间或世界协调时间。
大家都知道北京时区在时区划分上,属东八区,比协调世界时早8小时,记为UTC+8。当格林尼治时间为凌晨00:00时,中国标准时间刚好为上午08:00。
中国标准时间:GMT + 8 = UTC + 8 = CST
GMT与UTC区别
GMT和UTC都以伦敦格林威治时间为准,不同点是UTC比GMT更精准!
两者的区别在于前者是一个天文上的概念,而后者是基于一个原子钟。
在UTC中,每一年或两年会有一个“闰秒”,而我们一般不理会这个“闰秒”
JS 数据类型:字符串(String)、数字(Number)、布尔(Boolean)、数组(Array)、对象(Object)、日期(Date)、 Null、Undefined
JS 还拥有动态数据类型,var 定义.
其中String, Number, Boolean , Array , Object 等有 .prototype 属性,可以用来扩展数据类型的 方法和属性, 或者重写类型原来的方法或属性。
比如:
Number.prototype.Format = function(){ return "this is a number";} /// Format就是新扩展的方法,可以 Number类型数据就可以直接调用。
var a = 2;
a.Format() /// “this is a number”

浙公网安备 33010602011771号