Date 对象用于处理日期和时间。日期对象是用 new Date() 创建的。
实例化日期有四种方式:
var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
new Date() //2023-05-16T01:32:00.069Z object
new Date().getTime() //1684201632279 number
new Date().toISOString() //使用 ISO 标准将日期作为字符串返回, 2023-05-16T01:49:46.959Z string
new Date().toISOString().split('.')[0] //2023-05-16T01:49:46 string
new Date().toLocaleTimeString() //使用区域设置约定将 Date 对象的时间部分作为字符串返回,09:55:15 string
new Date().toLocaleDateString() //使用区域设置约定将 Date 对象的日期部分作为字符串返回, 2023/5/16 string
new Date().toJSON() //2023-05-16T02:05:49.250Z string
Date.now() //1684200720073 number
返回自 1970 年 1 月 1 日 00:00:00 (UTC) 到当前时间的毫秒数。
参考
changeTime(timestamp){ // 比如需要这样的格式 yyyy-MM-dd hh:mm:ss let date = new Date(timestamp * 1000) //时间戳为10位需*1000,时间戳为13位的话不需乘1000 let Y = date.getFullYear() + '-' let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-' let D = date.getDate() + ' ' let h = date.getHours() + ':' let m = date.getMinutes() + ':' let s = date.getSeconds() return Y+M+D+h+m+s },