export function getTime(){
let currentdate=[]
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
//以下代码依次是获取当前时间的年月日时分秒
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
var minute = date.getMinutes();
var hour = date.getHours();
var second = date.getSeconds();
//固定时间格式
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (hour >= 0 && hour <= 9) {
hour = "0" + hour;
}
if (minute >= 0 && minute <= 9) {
minute = "0" + minute;
}
if (second >= 0 && second <= 9) {
second = "0" + second;
}
currentdate=[parseInt(year),parseInt(month),parseInt(strDate),parseInt(hour),parseInt(minute),parseInt(second)]
return currentdate;
}
然后在main.js应用
import { getTime } from '@/utils/unit'
// 全局方法挂载
Vue.prototype.getTime=getTime
然后在text.vue页面
const currentTime=this.getTime()
//年月日 时分秒
const dataTime=currentTime[0]+"-"+currentTime[1]+"-"+
currentTime[2]+" "+currentTime[3]+" :"+currentTime[4]+":"+currentTime[5]
//年月
const dataTime=currentTime[0]+"/"+currentTime[1]