-
new Date() 参数
yyyy ∈ 四位数年数
month ∈ [January,December]
mth ∈ [0,11]
dd ∈ [1,31]
hh ∈ [0,23]
mm ∈ [0,59]
ss ∈ [0,59]
ms ∈ [0,+∞)
字符类型
new Date("month dd,yyyy hh:mm:ss");
new Date("month dd,yyyy");
new Date("yyyy-mth-dd");
new Date("yyyy/mth/dd");
new Date("yyyy,mth,dd");
举例:
new Date("July 12,2018 22:19:35"); //Thu Jul 12 2018 22:19:35 GMT+0800 (中国标准时间)
new Date("July 12,2018"); //Thu Jul 12 2018 00:00:00 GMT+0800 (中国标准时间)
new Date("2018-7-12"); //Thu Jul 12 2018 00:00:00 GMT+0800 (中国标准时间)
new Date("2018/7/12"); //Thu Jul 12 2018 00:00:00 GMT+0800 (中国标准时间)
new Date("2018,7,12"); //Thu Jul 12 2018 00:00:00 GMT+0800 (中国标准时间)
非字符类型
new Date(yyyy-mth-dd);
new Date(yyyy/mth/dd);
new Date(yyyy,mth,dd);
new Date(yyyy,mth,dd,hh,mm,ss);
new Date(ms);
举例:
new Date(2018-7-12); //Thu Jan 01 1970 08:00:01 GMT+0800 (中国标准时间)
new Date(2018/7/12); //Thu Jan 01 1970 08:00:00 GMT+0800 (中国标准时间)
new Date(2018,7,12); //Sun Aug 12 2018 00:00:00 GMT+0800 (中国标准时间),7对应的是八月
new Date(2018,7,12,22,19,35); //Sun Aug 12 2018 22:19:35 GMT+0800 (中国标准时间)
new Date(1521475200000); //Thu Jul 12 2018 16:06:26 GMT+0800 (中国标准时间)
补充
-
浏览器对参数的兼容性
| 参数\浏览器 | chrome | firfox | ie |
|---|---|---|---|
| "yyyy-mth-dd" | true | false | false |
| "yyyy/mth/dd" | true | true | true |
| "yyyy,mth,dd" | true | true | false |
| yyyy-mth-dd | true | true | true |
| yyyy/mth/dd | true | true | true |
| yyyy,mth,dd | true | true | true |
为了兼容,replace "/"
-
Date对象的方法
内置new Date()方法返回日期对象,常用方法:
var date = new Date(2018,7,12,22,19,35,666); // 返回日期对象
date.getYear(); // 获取当前年份(2位) 118
date.getFullYear(); // 获取完整的年份(4位,1970-????) 2018
date.getMonth(); // 获取当前月份(0-11,0代表1月) 7-代表八月
date.getDate(); // 获取当前日(1-31) 12
date.getHours(); // 获取当前小时数(0-23) 22
date.getMinutes(); // 获取当前分钟数(0-59) 19
date.getSeconds(); // 获取当前秒数(0-59) 35
date.getMilliseconds(); // 获取当前毫秒数(0-999) 666
date.getDay(); // 获取当前星期X(0-6,0代表星期天) 0
date.toLocaleString(); // 获取本地日期与时间 "2018/8/12 下午10:19:35"
date.toLocaleDateString(); // 获取本地当前日期 "2018/8/12"
date.toLocaleTimeString(); // 获取本地当前时间 "下午10:19:35"
date.toString(); // 获取日期与时间 "Sun Aug 12 2018 22:19:35 GMT+0800 (中国标准时间)"
date.toDateString(); // 获取当前日期 "Sun Aug 12 2018"
date.toLocaleTimeString(); // 获取当前时间 "22:19:35 GMT+0800 (中国标准时间)"
/*日期对象转时间戳*/
date.getTime(); // 获取当前时间(从1970.1.1开始的毫秒数) 1534083575000
date.valueOf(); // 前两者精确到毫秒 1534083575000
date.parse(); // 精确到秒
/*时间戳(13位)除以1000就可获得Unix时间戳*/
补充
-
对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
Date.prototype.Format = function (fmt) {
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()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
(new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18