回顾 前妻
目录
效果图:
解释时间用法(语法)
// 获取当前毫秒戳
console.log(new Date().getTime()); //ml:18 1638152442833
// 获取当前毫秒戳 1979 1 1日计算机出生日
console.log(Date.now()); //ml:18 1638152442833
// 需求将毫秒变时间 [1638152442833 ---> 2021年11月29日10:07:19]
console.log(new Date(1638153271716).toLocaleString()); // 自定义传毫秒数 2021/11/29 上午10:34:31
console.log(new Date(new Date().getTime()).toLocaleString()); //当前毫秒转时间 2021/11/29 上午10:34:31
// 需求将时间转为毫秒 [2021年11月29日10:07:19 ---> 1638152442833]
console.log((new Date("2021/11/29 10:21:11")).getTime()); //自定义时间得到豪秒数 ml:25 1638152471000
console.log(new Date().getTime()); //当前时间得到豪秒数 ml:25 1638152471000
// 2021/11/29 上午10:04:17 日期加时间
console.log(new Date().toLocaleString());
// 2021/11/29 日期
console.log(new Date().toLocaleDateString());
// 上午10:04:17 时间
console.log(new Date().toLocaleTimeString());
源代码复制即可用
<template>
<ul class="nowTime" ref="nowTime">
<li></li>
<li></li>
</ul>
</template>
<script setup lang="ts">
const nowTime = ref<HTMLDivElement | null>(null);
const NowTime = () => nowTime.value as HTMLDivElement;
//获取当前时间
const getNowFormatDate = () => {
let date = new Date();
let year: number | string = date.getFullYear();
let month: number | string = date.getMonth() + 1;
let strDate: number | string = date.getDate();
let Hour: number | string = date.getHours(); // 获取当前小时数(0-23)
let Minute: number | string = date.getMinutes(); // 获取当前分钟数(0-59)
let Second: number | string = date.getSeconds(); // 获取当前秒数(0-59)
let show_day = new Array(
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六"
);
let day = date.getDay();
if (Hour < 10) {
Hour = "0" + Hour;
}
if (Minute < 10) {
Minute = "0" + Minute;
}
if (Second < 10) {
Second = "0" + Second;
}
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
let currentdate =
"<div><p style='text-align:left;'>" +
year +
"年" +
month +
"月" +
strDate +
"号</p><p style='text-align:left'>" +
show_day[day] +
"</p></div>";
let HMS = Hour + ":" + Minute + ":" + Second;
let temp_time = year + "-" + month + "-" + strDate + " " + HMS;
NowTime().children[0].innerHTML = HMS;
NowTime().children[1].innerHTML = currentdate;
setTimeout(getNowFormatDate, 1000); //每隔1秒重新调用一次该函数
};
onMounted(() => {
getNowFormatDate();
})
</script>
<style lang="scss" scoped>
.nowTime {
position: absolute;
left: 10px;
top: 23px;
font-size: 0;
li {
display: inline-block;
width: 73px;
height: 21px;
font-size: 16px;
color: #000;
float: left;
}
li:nth-child(2) {
font-size: 9px;
p {
text-align: left;
}
}
}
</style>
结语:
祝大家2022越来越强
本文含有隐藏内容,请 开通VIP 后查看