先看看效果是你想要的不
下面直接贴代码 也简单 也是通过newData()
<template>
<div>
<p>{{ date_show }} - {{ week }}</p>
<p>{{ time_show }}</p>
</div>
</template>
<script>
export default {
data() {
return {
week: "", // 本周周几
date_show: "", //本日日期
time_show: "", //当前时间,时分秒
};
},
mounted() {
// 获取日期-星期
this.getdataTime();
// 读秒
setInterval(() => {
this.getnewTime();
}, 1000);
},
methods: {
// 获取当前系统日期
getdataTime() {
let wk = new Date().getDay();
let yy = new Date().getFullYear();
let mm = new Date().getMonth() + 1;
let dd = new Date().getDate();
let weeks = [
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六",
];
this.week = weeks[wk];
this.date_show = yy + "年" + mm + "月" + dd + "日";
},
// 获取当前系统的时间
getnewTime() {
let hh = new Date().getHours()< 10
? "0" + new Date().getHours()
: new Date().getHours();;
let mf =
new Date().getMinutes() < 10
? "0" + new Date().getMinutes()
: new Date().getMinutes();
let ss =
new Date().getSeconds() < 10
? "0" + new Date().getSeconds()
: new Date().getSeconds();
this.time_show = hh + ":" + mf + ":" + ss;
},
//简便写法
formatDate(data) {
let date = new Date(data)
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
month = (month > 9) ? month : ("0" + month);
day = (day < 10) ? ("0" + day) : day;
return year + "-" + month + "-" + day;
},
},
};
</script>
这个你可以直接复制到你的代码中 直接使用
格式转换 (Mon Dec 12 16:35:00 CST 2022)
function dateFormat(date, format) {
date = new Date(date);
date.setHours(date.getHours() - 14);
var o = {
'M+': date.getMonth() + 1, //month
'd+': date.getDate(), //day
'H+': date.getHours(), //hour
'm+': date.getMinutes(), //minute
's+': date.getSeconds(), //second
'q+': Math.floor((date.getMonth() + 3) / 3), //quarter
'S': date.getMilliseconds() //millisecond
};
if (/(y+)/.test(format))
format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp('(' + k + ')').test(format))
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
return format;
}
//方法调用
var s_time = dateFormat("Mon Dec 12 16:35:00 CST 2022", 'yyyy-MM-dd HH:mm:ss');
console.log(s_time);
原型链上的时间
Date.prototype.format = function (fmt) {
//yyyy-MM-dd hh:mm:ss:SSSS
var o = {
"M+": this.getMonth() + 1, //月
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
用法 (一秒获取一次最新的时间)可以根据需要获取年-月-日--时-分-秒
function updateTime() {
let currentTime = new Date().format("yyyy-MM-dd hh:mm:ss:SSS");
console.log(currentTime);
}
setInterval(updateTime, 1000);
字符串转时间
Date.prototype.parse = function (str, timezone = 'Asia/Shanghai') {
const date = new Date(str);
const timeZoneOffset = date.getTimezoneOffset();
const localTimezoneOffset = new Date().getTimezoneOffset();
const timeZoneDiff = (timeZoneOffset - localTimezoneOffset) * 60000;
const parsedDate = new Date(date.getTime() + timeZoneDiff);
return parsedDate.toLocaleString('zh-CN', { timeZone: timezone });
}
const dateString = '2023-05-06T10:30:00.000Z';
const timezone = 'America/New_York';
const parsedDate = new Date().parse(dateString, timezone);
console.log(parsedDate); // 输出转换后的本地时间格式
const dateString1 = '2023-05-06T10:30:00.000Z';
const timezone1 = 'Europe/London';
const parsedDate1 = new Date().parse(dateString, timezone);
console.log(parsedDate1); // 输出转换后的本地时间格式