JavaScript Date 常用API总结
let d = new Date()
第一类:Date 打印类函数(了解)
console.log("toLocaleString:", d.toLocaleString()) // 日期+时间 7/15/2023, 3:27:23 PM
console.log("toLocaleDateString:", d.toLocaleDateString()) // 日期 7/15/2023
console.log("toLocaleTimeString:", d.toLocaleTimeString()) // 时间 3:27:23 PM
console.log("\n中国标准时间格式")
console.log("toString:", d.toString()) // Sat Jul 15 2023 15:33:44 GMT+0800 (China Standard Time)
console.log("toTimeString:", d.toTimeString()) // 15:33:44 GMT+0800 (China Standard Time)
console.log("toDateString:", d.toDateString()) // Sat Jul 15 2023
console.log("\n全球标准时间格式")
console.log("toUTCString:", d.toUTCString()) // Sat, 15 Jul 2023 07:33:44 GMT
console.log("\nISO标准时间格式")
console.log("toISOString:", d.toISOString()) // 2023-07-15T07:33:44.224Z
第二类:Date 获取类函数
console.log(d.getDay()) // 6 它代表星期几,值为0-6,周日开始,周六结束
console.log(d.getFullYear()) // 2023
console.log(d.getMonth()) // 6 值为0-11
console.log(d.getHours()) // 15
console.log(d.getDate()) // 15
console.log(d.getMinutes()) // 43
console.log(d.getSeconds()) // 37
console.log(d.getMilliseconds()) // 809 毫秒
console.log(d.getTime()) // 1689411274044 时间戳
第三类:Date 设置类函数
d.setFullYear(2008)
d.setMonth(6) // 值为--11
d.setDate(5) // 设置天数
d.setHours(18)
d.setMinutes(30)
d.setSeconds(45)
console.log("日期设置后返回结果(中国标准格式):", d.toLocaleString()) // 7/5/2008, 6:30:45 PM
console.log("日期设置后返回结果 (时间戳):", d.getTime()) // 1215253845974
第四类: Date 解析类函数
// 字符串必须严格符合 ISO 8601 标准或者 RFC 2822 标准,否则会导致解析失败或结果不准确
// 以下是一些符合 ISO 8601 标准的日期时间字符串:
// 2023-07-15T12:13:14Z
// 2023-07-15T12:13:14.123Z
// 2023-07-15T12:13:14+08:00
// 2023-07-15T12:13:14.123+08:00
// RFC 2822 标准定义了电子邮件头部的格式,其中也包括日期和时间的表示方法。RFC 2822 标准支持多种日期时间格式,例如:
// Fri, 15 Jul 2023 12:13:14 +0800
// 15 Jul 2023 04:13:14 GMT
let str = '2023-07-15T12:00:00Z'
console.log(Date.parse(str)) // 1689422400000
第五类 常用方法封装
// 将中国标准时间 格式化为 YYYY-MM-DD HH:mm:ss
function dateFormat(value) {
const date = new Date(value)
return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`
}
console.log("格式化时间:", dateFormat(new Date().toString())) // 2023-07-15 16:54:34
// 将时间戳 格式化为 YYYY-MM-DD HH:mm:ss
function formatTimestamp(timestamp) {
let date = new Date(timestamp)
let year = date.getFullYear()
let month = padZero(date.getMonth() + 1)
let day = padZero(date.getDate())
let hour = padZero(date.getHours())
let minute = padZero(date.getMinutes())
let second = padZero(date.getSeconds())
return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}
// 月份补零
function padZero(num) {
return num.toString().padStart(2, '0')
}
// 根据当前日期和类型type获取日期的范围(时间戳)
// type说明 0: 本周;1上周;2:下周;3:本月;4:上月;5:下月
function getTimeRange(date, type) {
let start, end
switch (parseInt(type)) {
case 0:
start = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 1)
end = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 7, 23, 59, 59, 999)
break
case 1:
start = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() - 6)
end = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay(), 23, 59, 59, 999)
break
case 2:
start = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 8)
end = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 14, 23, 59, 59, 999)
break
case 3:
start = new Date(date.getFullYear(), date.getMonth(), 1)
end = new Date(date.getFullYear(), date.getMonth() + 1, 0, 23, 59, 59, 999)
break
case 4:
start = new Date(date.getFullYear(), date.getMonth() - 1, 1)
end = new Date(date.getFullYear(), date.getMonth(), 0, 23, 59, 59, 999)
break
case 5:
start = new Date(date.getFullYear(), date.getMonth() + 1, 1)
end = new Date(date.getFullYear(), date.getMonth() + 2, 0, 23, 59, 59, 999)
break
default:
throw new Error('无效的时间范围类型')
}
return {
start: start.getTime(),
end: end.getTime()
}
}
const obj = getTimeRange(new Date(), 0)
console.log(formatTimestamp(obj.start), formatTimestamp(obj.end)) // 2023-07-10 00:00:00 2023-07-16 23:59:59