## JS获取当前时间、及一周之前、一个月之前日期
```bash
//获取当前时间戳
const t1 = new Date().valueOf() // 第一种,推荐
const t2 = new Date().getTime() // 第二种,推荐
const t3 = Date.parse(new Date()) // 第三种,不推荐,精度差一些
//获取当前日期
getDay(day){
var today = new Date();
var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
today.setTime(targetday_milliseconds); //注意,这行是关键代码
var tYear = today.getFullYear();
var tMonth = today.getMonth();
var tDate = today.getDate();
tMonth = this.doHandleMonth(tMonth + 1);
tDate = this.doHandleMonth(tDate);
return tYear+""+tMonth+""+tDate;
},
//获取当前月份
doHandleMonth(month){
var m = month;
if(month.toString().length == 1){
m = "0" + month;
}
return m;
},
//一周之前
this.params.startDate=this.getDay(-7)
//两周之前
this.params.startDate=this.getDay(-14)
//一个月之前
this.params.startDate=this.getDay(-31)
//当前时间
this.params.endDate=this.getDay(0)
```
## 获取年月日加时分秒
```bash
getNowDate () {
var date = new Date();
var sign2 = ":";
var year = date.getFullYear() // 年
var month = date.getMonth() + 1; // 月
var day = date.getDate(); // 日
var hour = date.getHours(); // 时
var minutes = date.getMinutes(); // 分
var seconds = date.getSeconds() //秒
var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
var week = weekArr[date.getDay()];
// 给一位数的数据前面加 “0”
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (day >= 0 && day <= 9) {
day = "0" + day;
}
if (hour >= 0 && hour <= 9) {
hour = "0" + hour;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
if (seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds;
}
return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
}
```
## yyyyMMddHHmmssSSS最后在加一位1-10的随机数
```bash
function generateTimestamp() {
const date = new Date();
const year = date.getFullYear();
const month = ("0" + (date.getMonth() + 1)).slice(-2);
const day = ("0" + date.getDate()).slice(-2);
const hour = ("0" + date.getHours()).slice(-2);
const minute = ("0" + date.getMinutes()).slice(-2);
const second = ("0" + date.getSeconds()).slice(-2);
const millisecond = ("00" + date.getMilliseconds()).slice(-3);
const randomNumber = Math.floor(Math.random() * 10) + 1;
return year + month + day + hour + minute + second + millisecond + randomNumber;
}
```
## 计算两个日期之前的时间差
```bash
// 定义两个日期
var date1 = new Date("2022/01/01 00:00:00");
var date2 = new Date("2022/02/01 00:00:00");
// 计算两个日期之间的毫秒数差
var diff = Math.abs(date1.getTime() - date2.getTime());
// 将毫秒数转换为年、月、日、时、分、秒
var years = Math.floor(diff / (1000 * 60 * 60 * 24 * 365));
diff -= years * 1000 * 60 * 60 * 24 * 365;
var months = Math.floor(diff / (1000 * 60 * 60 * 24 * 30));
diff -= months * 1000 * 60 * 60 * 24 * 30;
var days = Math.floor(diff / (1000 * 60 * 60 * 24));
diff -= days * 1000 * 60 * 60 * 24;
var hours = Math.floor(diff / (1000 * 60 * 60));
diff -= hours * 1000 * 60 * 60;
var minutes = Math.floor(diff / (1000 * 60));
diff -= minutes * 1000 * 60;
var seconds = Math.floor(diff / 1000);
console.log(years + "年" + months + "个月" + days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒");
```
## 时间戳转换日期格式
```
//时间戳转换
const returnTime=(timestamp:any)=>{
var date = new Date(timestamp);
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2); // 前补0,确保月份始终是两位数
var day = ("0" + date.getDate()).slice(-2);
var hours = ("0" + date.getHours()).slice(-2);
var minutes = ("0" + date.getMinutes()).slice(-2);
var seconds = ("0" + date.getSeconds()).slice(-2);
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
```