// 获取一个当前时间的 Date 对象 var date = newDate(); // 从 1970年1月1日经过多少毫秒时间的 Date 对象 var date = newDate(1600000000000); // 以一个日期字符串创建一个 Date 对象 var date = newDate("2020-2-2 22:22:22.2222"); var date = newDate('December 17, 1995 03:24:00'); var date = newDate('1995-12-17T03:24:00'); // 以年月日时分秒毫秒数字创建 var date = newDate(1995, 11, 17, 3, 24, 0, 100);
常用操作
计算两个日期之间的相差(天/小时/分钟)
1 2 3 4 5 6
var date1 = newDate("2020-2-1 20:00:00"); var date2 = newDate("2020-2-2 20:00:00"); var diffTime = parseInt(date2.getTime() / 1000) - (date1.getTime() / 1000); var timeDay = parseInt(diffTime / 60 / 60 / 24); //相差天数 var timeHour = parseInt(diffTime / 60 / 60); //相差小时 var timeMinutes = parseInt(diffTime / 60); //相差分钟
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 (newRegExp("(" + k + ")").test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); } } return fmt; }
var formatDate = newDate().format("yyyy-MM-dd hh:mm:ss"); console.log(formatDate);
yyyy/MM/dd 格式
1 2
var date = newDate().toLocaleDateString(); console.log(date);
yyyy年MM月dd日 格式
其实这个格式把 ‘年月日’ 改成 ‘-‘ 就成了 yyyy-MM-dd hh:mm:ss 格式
1 2 3 4 5 6
var now = newDate(), y = now.getFullYear(), m = ("0" + (now.getMonth() + 1)).slice(-2), d = ("0" + now.getDate()).slice(-2); var date = y + "年" + m + "月" + d + "日" + now.toTimeString().substr(0, 8); console.log(date);
获取年、月、日、周、时、分、秒
1 2 3 4 5 6 7 8
var date = newDate(); date.getFullYear(); // 年 date.getMonth(); // 月 date.getDate(); // 日 date.getHours(); // 时 date.getMinutes(); // 秒