TS
const zeroPadd = (date) => {
var hours = date.getHours(); // 获取时
var minutes = date.getMinutes(); // 获取分
var seperator1 = "-"; // 自定义日期分隔符
var year = date.getFullYear(); // 获取年
var month = date.getMonth() + 1; // 获取月
var strDate = date.getDate(); // 获取日
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (hours >= 0 && hours <= 9) {
hours = "0" + hours;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
var time = hours + ":" + minutes; // 时分
var currentdate = year + seperator1 + month + seperator1 + strDate; // 年月日
var date = {
time, currentdate
}
return date;
}
// 获取当前日期 时间
const getNowFormatDate = () => {
var date = new Date();
return this.zeroPadd(date);
},
//获取一小时后的日期 时间
const accessTimeInAnHour = () => {
var date = new Date();
var date1 = new Date().getTime(); // 获取当前时间戳
// 当前时间戳+3600s(一小时,其他时间通过计算时间戳进行相应加减),重新设置 Date 对象
date.setTime(date1 + 3600000);
return this.zeroPadd(date);
},
JS
// 获取当前日期 时间
getNowFormatDate() {
var date = new Date();
return this.zeroPadd(date);
},
//获取一小时后的日期 时间
accessTimeInAnHour() {
var date = new Date();
var date1 = new Date().getTime(); // 获取当前时间戳
// 当前时间戳+3600s(一小时,其他时间通过计算时间戳进行相应加减),重新设置 Date 对象
date.setTime(date1 + 3600000);
return this.zeroPadd(date);
},
//日期时间补0
zeroPadd(date){
var hours = date.getHours(); // 获取时
var minutes = date.getMinutes(); // 获取分
var seperator1 = "-"; // 自定义日期分隔符
var year = date.getFullYear(); // 获取年
var month = date.getMonth() + 1; // 获取月
var strDate = date.getDate(); // 获取日
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (hours >= 0 && hours <= 9) {
hours = "0" + hours;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
var time = hours + ":" + minutes; // 时分
var currentdate = year + seperator1 + month + seperator1 + strDate; // 年月日
var date = {
time, currentdate
}
return date;
},
month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (hours >= 0 && hours <= 9) {
hours = "0" + hours;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}