一:获取时间对象new Date()
- new Date()不传参,获取当前时间对象
Wed Nov 25 2020 20:18:37 GMT+0800 (中国标准时间)
- 五种传参方式
- new Date(“month dd,yyyy hh:mm:ss”);
- new Date(“month dd,yyyy”);
- new Date(yyyy,mth,dd,hh,mm,ss);
- new Date(yyyy,mth,dd);
- new Date(ms);
例:
new Date("Nov 25,2020 20:18:37"); //Wed Nov 25 2020 20:18:37 GMT+0800 (中国标准时间)
new Date("Nov 25,2020"); //Wed Nov 25 2020 00:00:00 GMT+0800 (中国标准时间)
new Date(2020,10,25,20,18,37); //Wed Nov 25 2020 20:18:37 GMT+0800 (中国标准时间)
new Date(2020,10,25); //Wed Nov 25 2020 00:00:00 GMT+0800 (中国标准时间)
new Date(1606306717000); //Wed Nov 25 2020 20:18:37 GMT+0800 (中国标准时间)
注意点:
month可用英文单词或英文缩写,0-11对应1-12
填写日的参数,如果写0,代表上个月的最后一天,1代表这个月的第一天,如果写的数字大于这个月的天数,会一直加到月份上,然后剩余的天数代表这个月的第几天,
例如:new Date(2020,10,0)
,代表10月31,new Date(2020,10,1)
代表11月1日
二:常用时间获取方法
- Date() 返回当日的日期和时间。
- getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
- getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。(周日为0;)
- getMonth() 从 Date 对象返回月份 (0 ~ 11)。
- getFullYear() 从 Date 对象以四位数字返回年份。
- getYear() 请使用 getFullYear() 方法代替。
- getHours() 返回 Date 对象的小时 (0 ~ 23)。
- getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。
- getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。
- getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。
- getTime() 返回 1970 年 1 月 1 日至今的毫秒数。
三:日期设置方法
- setDate() 设置 Date 对象中月的某一天 (1 ~ 31)。
- setMonth() 设置 Date 对象中月份 (0 ~ 11)。
- setFullYear() 设置 Date 对象中的年份(四位数字)。
- setYear() 请使用 setFullYear() 方法代替。
- setHours() 设置 Date 对象中的小时 (0 ~ 23)。
- setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)。
- setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。
- setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。
- setTime() 以毫秒设置 Date 对象。
四:时间转字符串
- toString() 把 Date 对象转换为字符串。
Wed Nov 25 2020 20:45:21 GMT+0800 (中国标准时间)
- toTimeString() 把 Date 对象的时间部分转换为字符串。
20:45:34 GMT+0800 (中国标准时间)
- toDateString() 把 Date 对象的日期部分转换为字符串。
Wed Nov 25 2020
- toUTCString() 根据世界时,把 Date 对象转换为字符串。
Wed, 25 Nov 2020 12:57:50 GMT
- toLocaleString() 根据本地时间格式,把 Date 对象转换为字符串。
2020/11/25 下午8:50:58
- toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串。
下午8:51:04
- toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。
2020/11/25
获取今天刚开始的时间戳:new Date(new Date().toLocaleDateString()).getTime()
五:常用时间段
昨天,今天,上周,本周,上月,本月,过去三天,过去七天,过去三十天
function timeDiff (start, time) {
return start.setTime(start.getTime() - 3600 * 1000 * 24 * time)
}
let now = new Date()
let todayStart = new Date(now.toLocaleDateString()).getTime()
let todayEnd = timeDiff(new Date(todayStart - 1), -1)
let monthStart = new Date(now.getFullYear(), now.getMonth(), 1).getTime()
let arr = [
{text: '昨天', time: [timeDiff(new Date(todayStart), 1), todayStart - 1]},
{text: '今天', time: [todayStart, +now]},
{text: '上周', time: [timeDiff(new Date(todayStart), now.getDay() + 6), timeDiff(new Date(todayEnd), now.getDay())]},
{text: '本周', time: [timeDiff(new Date(todayStart), now.getDay() - 1), +now]},
{text: '上月', time:[
new Date(now.getFullYear(), now.getMonth() - 1 === '-1' ? 11 : now.getMonth() - 1, 1).getTime(),
monthStart - 1]
},
{text: '本月', time: [monthStart, +now]},
{text: '过去3天', time: [timeDiff(new Date(todayStart), 2), +now]},
{text: '过去7天', time: [timeDiff(new Date(todayStart), 6), +now]},
{text: '过去30天', time: [timeDiff(new Date(todayStart), 29), +now]}
]
注:早上的时间戳减去1为前一天的最后一毫秒时间戳,本月刚开始的时间戳减去1为上个月最后一秒时间戳
六:常用moment方法
1、时间戳转时间(格式化)
使用moment(时间戳数据).format()
例如
moment(1568712361000).format('YYYY-MM-DD HH:mm:ss')
//2019-09-17 17:26:01
YYYY年MM月DD日
HH时mm分ss秒 //HH 代表24小时制,hh代表12小时制
hh:mm:ss a
2、时间数据转时间戳
一般用作向后端发送数据,将时间数据转换为number类型的时间戳。
moment(时间数据).valueOf()
3、获取时间
//moment()中有值代表获取当天时间,否则是今天的时间
moment().startOf('day') //0时0分0秒
moment().endOf('day') //23时59分59秒
moment().startOf('week') //周一0时0分0秒
moment().endOf('week') // 周六23时59分59秒
moment().endOf('isoWeek') //周日23时59分59秒
moment().startOf('month') // 月第一天0时0分0秒
moment().endOf('month') //月最后一天23时59分59秒
4、moment与ant
1、RangePIcker中属性showTime中默认值
<RangePicker
showTime={{ defaultValue: moment('00:00:00', 'HH:mm:ss') }}
/>
5、时间戳转换10位与13位
13位转10位时间戳
moment(1318874398806).unix(); // 1318874398
10位转13位可以直接*1000,转换为时间格式
基于UTC
moment.unix(1318781876).utc(); // 后三位为0
正常使用
moment.unix(Number)
moment.unix(1318781876.721);后三位为小数点后面三位