new Date支持的参数:javascript
MDN:php
new Date();
html
new Date(value);
java
new Date(dateString);
windows
new Date(year, month, day, hour, minute, second, millisecond);
ui
MSDN:
spa
dateObj = new Date()code
dateObj = new Date(dateVal)orm
dateObj = new Date(year, month, date[, hours[, minutes[, seconds[,ms]]]]) htm
不过在MSDN中把dateVal当作了MDN中参数为value和dateString的合集。其实是同样的。
对于无参数和多参数的使用方法除了month参数是从0开始计算以外没有什么特殊用法。
若是参数为数字时表明从1970年1月1日算起的毫秒数:Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch)。最麻烦,可是使用起来也是最方便的字符串参数,在MDN中:
The string should be in a format recognized by theDate.parse()
method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601).
在MSDN中:
dateVal is parsed according to the rules in Date and Time Strings (JavaScript). The dateVal argument can also be a VT_DATE value as returned from some ActiveX objects.
一句话来讲,就是对于经过JS Date对象以及相关方法(UTC、ISO、GMT等)生成的各类字符串均可以解析。除此以外,经常使用的使用方法以下:
/*须要注意:月份此时从1开始计算*/ new Date("2014-04-11"); // Fri Apr 11 2014 08:00:00 GMT+0800 (中国标准时间)
注:该方式不推荐,由于在windows下的safari 5及如下版本不能正确解析,会返回NaN。而在Mac下的Safari则没有此问题。不过若是忽略安装Safari的windows用户,这个方法也不错。
通常咱们可能使用new Date(YYYY,MM,DD)或者set方法来建立对象,不过经过set方法在某些特殊日期下会出错:
/*bad!*/ var date = new Date(2014,2,30); // Fri Mar 30 2014 00:00:00 GMT+0800 (中国标准时间) date.setMonth(1); // Fri Mar 2 2014 00:00:00 GMT+0800 (中国标准时间) date.setDate(28); // Fri Mar 28 2014 00:00:00 GMT+0800 (中国标准时间) var date = new Date(2014,1,28); // Fri Feb 28 2014 00:00:00 GMT+0800 (中国标准时间) date.setDate(30); // Sun Mar 02 2014 00:00:00 GMT+0800 (中国标准时间) date.setMonth(3); // Wed Apr 02 2014 00:00:00 GMT+0800 (中国标准时间) /*better!*/ var dateNew = new Date(2014,1,28); // Fri Feb 28 2014 00:00:00 GMT+0800 (中国标准时间) var dateNew2 = new Date(2014,3,30); // Wed Apr 30 2014 00:00:00 GMT+0800 (中国标准时间)
PS: ISO Date Format is not supported in Internet Explorer 8 standards mode and Quirks mode.
参考文章:
一、https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
二、https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
三、http://tools.ietf.org/html/rfc2822#page-14
四、http://www.w3.org/TR/NOTE-datetime
五、http://msdn.microsoft.com/en-us/library/ie/cd9w2te4(v=vs.94).aspx
六、http://msdn.microsoft.com/en-us/library/ie/ff743760(v=vs.94).aspx
七、http://www.php100.com/manual/Javascript/html/jsobjdate.htm