建议写成工具函数导出
common.js : export formatDateFull(time) {...}
import { formatDateFull, formatDate } from '@/utils/common'
-
时间戳转换成YYYY-MM-DD HH:MM:SS格式
function formatDateFull(time) {
var val= time ? new Date(time) : new Date();
var Y = `${val.getFullYear()}`;
var M = `${val.getMonth()+1<10 ? '0'+(val.getMonth()+1) : val.getMonth()+1}`;
var D = `${val.getDate()<10 ? '0'+val.getDate() : val.getDate()}`
var h = `${val.getHours()<10 ? '0'+val.getHours() : val.getHours()}`
var m = `${val.getMinutes()<10 ? '0'+val.getMinutes() : val.getMinutes()}`
var s = `${val.getSeconds()<10 ? '0'+val.getSeconds() : val.getSeconds()}`
return `${Y}-${M}-${D} ${h}:${m}:${s}`
}
-
时间戳转换成YYYY-MM-DD格式
function formatDate(time) {
var val= time ? new Date(time) : new Date();
var Y = `${val.getFullYear()}-`;
var M = `${val.getMonth()+1<10 ? '0'+(val.getMonth()+1) : val.getMonth()+1}-`;
var D = `${val.getDate()<10 ? '0'+val.getDate() : val.getDate()}`
return Y + M + D
}
-
时间戳转换成前一个月的时间格式
function formatLastMonthDate(val) {
var val=new Date()
var nowdate=new Date()
val.setMonth(nowdate.getMonth()-1);
const Y = `${val.getFullYear()}-`;
const M = `${val.getMonth()+1<10 ? '0'+(val.getMonth()+1) : val.getMonth()+1}-`;
const D = `${val.getDate()} `
return Y + M + D
}
-
上周的时间格式
function formatLastWeekDate(val) {
var nowdate=new Date()
var val = new Date(nowdate-7*24*3600*1000);
const Y = `${val.getFullYear()}-`;
const M = `${val.getMonth()+1<10 ? '0'+(val.getMonth()+1) : val.getMonth()+1}-`;
const D = `${val.getDate()} `
return Y + M + D
}
-
将秒数转换成时/分/秒
function formateSec(num) {
if (num < 60) {
return `${num}秒`
} else {
var min = parseInt(num / 60);
min = min < 10 ? `0${min}` : min;
var sec = parseInt(num % 60);
sec = sec < 10 ? `0${sec}` : sec
if (min >= 60) {
var hour = parseInt(min / 60);
var min = parseInt(min % 60);
hour = hour < 10 ? `0${hour}` : hour
return `${hour}时${min}分${sec}秒`
} else {
return `${min}分${sec}秒`
}
}
}
-
获取开始时间
//startTimeSetIndex是判断选择是现在,还是自定义
geBeginTime(){
let res = '';
if(this.startTimeSetIndex == 0){
res = formatDateFull()
// 去掉秒
res = res.substring(0, res.lastIndexOf(':'))
}else if(this.startTimeSetIndex == 1){
res = this.startTime
}
return res
},
-
获取截止时间
//endTimeSetIndex判断是今天,还是明天,还是自定义
getEndTime(){
let now = +new Date();
let t = 0;
let res = '';
//
if(this.endTimeSetIndex == 0){
// 今天
res = `${formatDate()} 22:00`
}else if(this.endTimeSetIndex == 1){
// 明天年月日
t = now + 1 * 24 * 60 * 60 * 1000
res = res.substring(0, res.lastIndexOf(':'))
}else if(this.endTimeSetIndex == 3){
res = this.endTime
}
return res
},