ts外部引入获取当前时间或一小时后
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a8d8e627ace7f693412293e5cec2245b.png#pic_center)
在index.ts 中
// 获取格式化后的时间
export const getFormatTime = (date: Date) => {
const year = date.getFullYear() // 获取年
let month = date.getMonth() + 1 // 获取月
let strDate = date.getDate() // 获取日
let hours = date.getHours() // 获取时
let minutes = date.getMinutes() // 获取分
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;
}
const time = hours + ":" + minutes; // 时分
const currentdate = year + "-" + month + "-" + strDate; // 年月日
const dates = {
time, currentdate
}
return dates;
}
//获取一小时后的时间
export const getOneHours = () => {
const date = new Date();
const date1 = new Date().getTime(); // 获取当前时间戳
date.setTime(date1 + 1000*60*60);
return getFormatTime(date);
}
在index.vue中使用
import { getFormatTime,getOneHours} from './index';
打印效果
console.log(getFormatTime(new Date()));
console.log(getOneHours())