文章目录
- JavaScript 日期对象
-
- 创建日期对象
- 常用方法
- 格式化
-
- 验证日期是否合法
- 日期计算
-
- 比较两日期大小
- 计算日期前后N天的日期
- 计算两日期的时间差
- 倒计时功能
JavaScript 日期对象
创建日期对象
var obj = new Date();
console.log(obj);
var obj = new Date(2008, 9, 10, 12, 13, 14);
console.log(obj);
var obj = new Date('2008-9-10 11:12:13');
console.log(obj);
常用方法
方法 |
说明 |
getFullYear() |
获取年份 |
getMonth() |
获取月份,0~11之间 |
getDate() |
获取日数,1~31之间 |
getHours() |
获取时,0~23之间 |
getMinutes() |
获取分,0~59之间 |
getSeconds() |
获取秒,0~59之间 |
setFullYear() |
可以设置年、月、日 |
setMonth() |
可以设置月、日 |
setHours() |
可以设置时、分、秒、毫秒 |
setMinutes() |
可以设置分、秒、毫秒 |
setSeconds() |
可以设置秒、毫秒 |
getDay() |
获取星期几,0表示周末,1表示周一 ~ 6表示周六 |
getTime() |
获取时间戳 |
Date.now() |
获取当前日期时间戳 |
var obj = new Date();
console.log(obj);
var year = obj.getFullYear();
var month = obj.getMonth()
var date = obj.getDate();
var hours = obj.getHours();
var minutes = obj.getMinutes();
var seconds = obj.getSeconds();
console.log(year + "-" + month + "-" + date);
console.log(hours + ":" + minutes + ":" + seconds);
var obj = new Date();
obj.setFullYear(2008);
obj.setMonth(8);
obj.setDate(9);
obj.setHours(8);
obj.setMinutes(8);
obj.setSeconds(10);
console.log(obj);
var weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
var obj = new Date();
var day = obj.getDay();
console.log("今天是" + weeks[day]);
var obj = new Date();
var time = obj.getTime();
console.log(time);
格式化
日期时间格式化
Date.prototype.format = function(pattern) {
function fillZero(num) {
return num < 10 ? "0" + num : num;
}
var pattern = pattern;
var dateObj = {
"y": this.getFullYear(),
"M": fillZero(this.getMonth() + 1),
"d": fillZero(this.getDate()),
"H": fillZero(this.getHours()),
"m": fillZero(this.getMinutes()),
"s": fillZero(this.getSeconds())
};
return pattern.replace(/yyyy|MM|dd|HH|mm|ss/g, function(match) {
switch (match) {
case "yyyy":
return dateObj.y;
case "MM":
return dateObj.M;
case "dd":
return dateObj.d;
case "HH":
return dateObj.H;
case "mm":
return dateObj.m;
case "ss":
return dateObj.s;
}
});
};
var d = new Date();
console.log(d.format('yyyy-MM-dd HH:mm:ss.S'));
console.log(d.format('yyyy-MM-dd'));
console.log(d.format('yyyy-MM-dd q HH:mm:ss'));
console.log(d.format("yyyy年MM月dd日 HH时mm分ss秒"));
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() + "").substring(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]).substring(("" + o[k]).length)));
return fmt;
};
var d = new Date();
console.log(d.format('yyyy-MM-dd HH:mm:ss.S'));
console.log(d.format('yyyy-MM-dd'));
console.log(d.format('yyyy-MM-dd q HH:mm:ss'));
console.log(d.format("yyyy年MM月dd日 HH时mm分ss秒"));
使用moment.js格式化
<script src="js/moment.js">script>
<script>
console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));
console.log(moment().format('dddd'));
console.log(moment().format("MMM Do YY"));
console.log(moment().format('YYYY [escaped] YYYY'));
console.log(moment().format('yyyy-MM-DD HH:mm:ss'));
console.log(moment("20111031", "YYYYMMDD").fromNow());
console.log(moment("20120620", "YYYYMMDD").fromNow());
console.log(moment().startOf('day').fromNow());
console.log(moment().endOf('day').fromNow());
console.log(moment().startOf('hour').fromNow());
script>
验证日期是否合法
function validateDate(str) {
var reg = /^(\d+)-(\d{1,2})-(\d{1,2})$/;
var r = str.match(reg);
if (r == null) return false;
r[2] = r[2] - 1;
var d = new Date(r[1], r[2], r[3]);
if (d.getFullYear() != r[1]) return false;
if (d.getMonth() != r[2]) return false;
if (d.getDate() != r[3]) return false;
return true;
}
console.log(validateDate('2018-08-20'));
console.log(validateDate('2018-08-40'));
日期计算
比较两日期大小
function compareDate(dateStr1, dateStr2) {
var date1 = dateStr1.replace(/-/g, "\/");
var date2 = dateStr2.replace(/-/g, "\/");
return new Date(date1) > new Date(date2);
}
var dateStr1 = "2018-07-30 7:31";
var dateStr2 = "2018-07-31 7:30";
var dateStr3 = "2018-08-01 17:31";
var dateStr4 = "2018-08-01 17:30";
console.log(compareDate(dateStr1, dateStr2));
console.log(compareDate(dateStr3, dateStr4));
计算日期前后N天的日期
function GetDateStr(AddDayCount) {
var dd = new Date();
dd.setDate(dd.getDate() + AddDayCount);
var y = dd.getFullYear();
var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1);
var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
return y + "-" + m + "-" + d;
}
console.log("半年前:" + GetDateStr(-180));
console.log("三月前:" + GetDateStr(-90));
console.log("一月前:" + GetDateStr(-30));
console.log("昨天:" + GetDateStr(-1));
console.log("今天:" + GetDateStr(0));
console.log("明天:" + GetDateStr(1));
console.log("后天:" + GetDateStr(2));
console.log("一月后:" + GetDateStr(30));
console.log("三月后:" + GetDateStr(90));
console.log("半年后:" + GetDateStr(180));
计算两日期的时间差
function GetDateDiff(startTime, endTime, diffType) {
startTime = startTime.replace(/\-/g, "/");
endTime = endTime.replace(/\-/g, "/");
diffType = diffType.toLowerCase();
var sTime = new Date(startTime);
var eTime = new Date(endTime);
var divNum = 1;
switch (diffType) {
case "second":
divNum = 1000;
break;
case "minute":
divNum = 1000 * 60;
break;
case "hour":
divNum = 1000 * 3600;
break;
case "day":
divNum = 1000 * 3600 * 24;
break;
default:
break;
}
return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
}
var result1 = GetDateDiff("2018-07-30 18:12:34", '2018-08-01 9:17:30', "day");
var result2 = GetDateDiff("2018-07-29 20:56:34", '2018-08-01 9:17:30', "hour");
console.log("两者时间差为:" + result1 + "天。");
console.log("两者时间差为:" + result2 + "小时。");
倒计时功能
function countDown(year, month, date, hour, minutes, seconds) {
month--;
var nowTime = Date.now();
var targetTime = new Date(year, month, date, hour, minutes, seconds);
var millisecond = targetTime - nowTime;
var totalSeconds = (millisecond) / 1000;
var d = parseInt(totalSeconds / 60 / 60 / 24);
var h = parseInt(totalSeconds / 60 / 60 % 24);
var m = parseInt(totalSeconds / 60 % 60);
var s = parseInt(totalSeconds % 60);
return d + '天' + h + '时' + m + '分' + s + '秒';
}
console.log(countDown(2022, 12, 15, 18, 0, 0));