一.Date对象简介
1 使用时必须使用new来调用创建我们的日期对象(既构造对象)
构造当前时间对象主要有以下4中方法:
var MyDate = new Date()
var MyDate = new Date(milliseconds)
var MyDate = new Date(string)
var MyDate = new Date(y, m, d, h, min, sec, ms)
2 创建date的时候,传入要设置的时间参数
参数形式有以下5种:
参数 | 格式(以2021年10月19日) |
---|---|
new Date(“month dd,yyyy hh:mm:ss”) | new Date(“October 19,2021 11:05:35”) |
new Date(“month dd,yyyy”) | new Date(“October 19,2021”) |
new Date(yyyy,mth,dd,hh,mm,ss) | new Date(2021,9,19,11,09,35) |
new Date(yyyy,mth,dd) | new Date(2021,9,19) |
new Date(ms) | new Date(1634613216674) |
返回的都是中国标准时间
参数简述
month:用英文 表示月份名称,从January到December
mth:用整数表示月份,从0(1月)到11(12月)
dd:表示一个 月中的第几天,从1到31
yyyy:四位数表示的年份
hh:小时数,从0(午夜)到23(晚11点)
mm: 分钟数,从0到59的整数
ss:秒数,从0到59的整数
ms:毫秒数,为大于等于0的整数
二.日期时间方法
使用new Date获取一个时间对象:var date = new Date()
1 时间对象
Date方法 | 简介 | 使用 |
---|---|---|
date | (中国标准时间) | console.log(date);// Tue Oct 19 2021 11:37:44 GMT+0800 (中国标准时间) |
date.toLocaleString() | 获取本地日期时间 | console.log(date.toLocaleString());// 2021/10/19 上午11:39:57 |
date.toLocaleDateString() | 获取本地日期 | console.log(date.toLocaleDateString());// 2021/10/19 |
date.toLocaleTimeString() | 获取本地时间 | console.log(date.toLocaleTimeString());// 上午11:42:12 |
2 时间的获取方法
Date方法 | 简介 | 使用 |
---|---|---|
date.getDate() | 返回几号 (1 ~ 31) | console.log(date.getDate());//19 |
date.getDay() | 返回星期几 (0 ~ 6),星期日为0 | console.log(date.getDay());//2 |
date.getMonth() | 返回月份 (0 ~ 11),从0开始 | console.log(date.getMonth());//9 |
date.getFullYear() | 返回四位数字的年份 | console.log(date.getFullYear());//2021 |
date.getYear() | 两位数的年份,从1900年开始 | console.log(date.getYear());//121 |
date.getHours() | 返回小时 (0 ~ 23) | console.log(date.getHours());//22 |
date.getMinutes() | 返回分钟 (0 ~ 59) | console.log(date.getMinutes());//54 |
date.getSeconds() | 返回秒 (0 ~ 59) | console.log(date.getSeconds());//29 |
date.getMilliseconds() | 返回毫秒(0 ~ 999) | console.log(date.getMilliseconds());//0 |
date.getTime() | 返回 1970 年 1 月 1 日至今的毫秒数 | console.log(date.getTime());//1634622998044 |
Date扩展
1 格式化日期
xxxx年xx月xx日星期x (2020年6月29日星期一)
Date.prototype.format1st = function (fmt) {
var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
var o = {
"M+": this.getMonth() + 1 + '月', //月份
"d+": this.getDate() + '日', //日
"a+": arr[this.getDay()]
};
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;
}
console.log((new Date()).format1st("yyyyMda")); // 2021年10月19日星期二
xxxx-xx-xx xx:xx:xx (2020-01-01 08:00:07.523)
对Date的扩展,将 Date 转化为指定格式的String
月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
为方便我们对日期(Date)进行格式化输出,先对 Date 进行扩展,增加 format 方法。以后调用 Date 对象的 format 方法即可将日期转换成我们指定格式的字符串(String)。
Date.prototype.format2nd = 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;
}
// 2021-10-19 14:13:08.296
console.log((new Date()).format2nd("yyyy-MM-dd hh:mm:ss.S"));
// 2021-10-19 14:13:8.296
console.log((new Date()).format2nd("yyyy-M-d h:m:s.S"));
console.log((new Date()).format2nd("yyyy-MM-dd hh:mm:ss")); //2021-10-19 14:14:03 (常用)
// 格式化输出指定时间
var date = new Date("2021-10-19 14:13:8.296");
console.log(date.format2nd("MM-dd hh:mm")); // //10-19 14:13
注:RegExp
RegExp 是javascript中的一个内置对象。为正则表达式。
RegExp.$1是RegExp的一个属性,指的是与正则表达式匹配的第一个 子匹配(以括号为标志)字符串,以此类推,RegExp.$2,RegExp.$3,…RegExp.$99总共可以有99个匹配