这是我参与11月更文挑战的第9天,活动详情查看:2021最后一次更文挑战
获取时间是我们开发经常用到,今日就来看下各种获取时间的方法,还有一些特别的处理方法,来做一个汇总。
var date = new Date()
声明Date对象date所指的月份中的某一天,使用本地时间。返回值是1~31之间的 一个整数。
date.getDate()
声明Date对象date所指的一个星期中的某一天,使用本地时间。返回值是0(周日) 到6(周六)之间的一个整数。
date.getDay()
当dace用本地时间表示时返回的年份。返回值是一个四位数,表示包括世纪值在内的完整年份。也可以获取返回两位数的年份
date.getFullYear() //四位数年份
date.getYear(); //两位数年份
声明Date对象date的小时字段,以本地时间表示。返回值是0(午夜)到23(晚上 11点)之间的一个整数。
date.getHours()
声明Date对象date的毫秒字段,用本地时间表示。返回值在0~999之间。
date.getMilliseconds()
声明的Date对象date的分钟字段,以本地时间表示。返回值在0~59之间。
date.getMinutes()
声明的Date对象date的月份字段,以本地时间表示。返回值在0(一月)到11(十二月)之间。
date.getMonth()
//正常的月份 应该要+1 date.getMonth()+1
声明的Date对象date的秒字段,以本地时间表示。返回值在0~59之间。
date.getSeconds()
声明的Date对象date的毫秒表示,也就是date指定的日期和时间距从1970.1.1开始的毫秒数
date.getTime()
声明的日期和时间距1970年1月1日午夜(GMT时间)之间的毫秒数。
Date.parse(date)
//1280977330000
date的日期部分的字符串表示,由实现决定,可以读懂,以本地时间表示
date.toDateString()
date的日期部分的字符串表示,由实现决定,可以读懂,以本地时间表示,根 据本地规约格式化。
date.toLocaleDateString()
console.log(date.toDateString());
console.log(date.toLocaleDateString());
console.log(date.toLocaleString());
console.log(date.toLocaleTimeString());
console.log(date.toString());
console.log(date.toTimeString());
console.log(date.valueOf()); //返回毫秒数
//打印结果如下
// Fri Nov 05 2021
// 2021/11/5
// 2021/11/5 下午5:36:27
// 下午5:36:27
// Fri Nov 05 2021 17:39:29 GMT+0800 (中国标准时间)
// 17:39:29 GMT+0800 (中国标准时间)
// 1636105169501
在javascript中取date的前一天时间:
new Date(new Date()-24*60*60*1000),//取前一天的时间
获取 2021-01-11格式的时间
let year = new Date().getFullYear()
let month = new Date().getMonth() + 1
let month2 = (month.toString().length > 1 ? month : '0'+month).toString() //给月份补0
let day = new Date().getDate()
let day2 = (day.toString().length > 1 ? day : '0'+day).toString() //给日补0
let date = year + '-' + month2 + '-' + day2
//2021-01-11