1 时间格式化
代码语言:javascript复制function strftime(date) {
let dateee = new Date(date).toJSON();
return new Date( new Date(dateee) 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/.[d]{3}Z/, '')
}
console.log(strftime("2022-05-20T12:24:18.000 0000"))
// 输出:2022-05-20 20:24:18
2 获取当前时间戳
代码语言:javascript复制let timestamp1 = Date.parse(new Date())
console.log(timestamp1)
// 1667467418000
let timestamp2 = (new Date()).valueOf()
console.log(timestamp2)
// 1667467418100
let timestamp3 = new Date().getTime()
console.log(timestamp3)
// 1667467418100
let timestamp4 = Number(new Date())
console.log(timestamp4)
// 1667467418100
3 时间戳转时间
代码语言:javascript复制// 时间戳为毫秒 13位
let now = new Date(1653038423000),
y = now.getFullYear(),
m = now.getMonth() 1,
d = now.getDate(),
res = y "-" (m < 10 ? "0" m : m) "-" (d < 10 ? "0" d : d) " " now.toTimeString().substr(0, 8);
console.log(res);
// 输出:2022-05-20 17:20:23
代码语言:javascript复制<script src="js/jquery-min.js"></script>
<script>
function time() {
dt = new Date();
var year = dt.getFullYear();
var month = dt.getMonth();
var date = dt.getDate();
// alert(year ' ' month '' date)
var h = dt.getHours();//获取时
var m = dt.getMinutes();//获取分
var s = dt.getSeconds();//获取秒
var wee = new Array("日 ", "一 ", "二 ", "三 ", "四 ", "五 ", "六 ");
var weekstr = wee[new Date().getDay()];
document.getElementById("showTime").innerHTML = "当前时间为:" year "年" month "月" date "日" h "时" m "分" s "秒 星期" weekstr;
setTimeout("time()", 1000); //设定定时器,循环运行
}
</script>
代码语言:html复制<span id="showTime"></span>
<script>
time();
</script>
输出:当前时间为:2021年1月20日14时31分51秒 星期六