1.格林威治时间(GMT)
Tue, 29 Nov 2022 03:30:28 GMT // GMT 时间
Tue Nov 29 2022 11:30:48 GMT+0800 (中国标准时间) // 本地时间
2.世界标准时(UTC)
2019-11-11T00:00:00.000Z
2019-11-11T00:00:00.000UTC
2019-11-11T00:00:00.000+08:00
3.中国标准时间(CST)
Central Standard Time (USA) UT-6:00
Central Standard Time (Australia) UT+9:30 China Standard Time UT+8:00
Cuba Standard Time UT-4:00
4.ISO 8601 标准时间
2022-11-29T03:34:26.567Z // ISO 8601
2022-11-29T11:34:26.567Z+08:00 // 本地时间
// 字母T:分割日期和时间
// 而786表示的毫秒
// Z表示UTC统一时间
5.时间戳(从格林威治时间 1970年01月01日00时00分00秒 起 至 现在 的总秒数)
1669383136370
时间格式互相转换
创建时间对象Date
创建时间对象输出的是当前的中国标准时间
let date = new Date()
时间戳转换为标准时间格式
let date = new Date(timestamp)
标准时间格式转换为时间戳
let timestamp = new Date().getTime()
时间格式化方法
new Date().toDateString() // 'Fri Nov 25 2022'
new Date().toTimeString() // '21:51:53 GMT+0800 (中国标准时间)'
new Date().toLocaleDateString() // '2022/11/25'
new Date().toLocaleTimeString() // '21:57:18'
new Date().toUTCString() // 'Fri, 25 Nov 2022 13:57:36 GMT'
new Date().toGMTString() // 'Fri, 25 Nov 2022 13:59:23 GMT'
new Date().toISOString() // '2022-11-25T13:30:39.593Z'
// toLocaleString方法
new Date().toLocaleString() // '2022/11/28 18:21:16'
下面是一个标准时间转换为yy-mm-dd hh:mm:ss的方法
const formaDate = (inputDateTimeString) => {
// 将字符串解析为日期对象
const inputDate = new Date(inputDateTimeString);
// 提取所需的年、月、日、小时、分钟和秒
const year = inputDate.getUTCFullYear();
const month = String(inputDate.getUTCMonth() + 1).padStart(2, "0");
const day = String(inputDate.getUTCDate()).padStart(2, "0");
const hours = String(inputDate.getUTCHours()).padStart(2, "0");
const minutes = String(inputDate.getUTCMinutes()).padStart(2, "0");
const seconds = String(inputDate.getUTCSeconds()).padStart(2, "0");
const formattedDateTimeString = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
return formattedDateTimeString
}
padStart是自动补全字符串的方法,如果字符串长度大于或等于2则无效果,否则在字符串前面补0。同理还有padEnd