年月日
function yearToDay(time) {
var y = time.getFullYear(),
m = time.getMonth() 1,
d = time.getDate();
m = m < 10 ? "0" m : m;
d = d < 10 ? "0" d : d;
return y "-" m "-" d;
};
//2022-03-31
function yearToSecond(time) {
var h = time.getHours(),
i = time.getMinutes(),
s = time.getSeconds(),
h = h < 10 ? "0" h : h;
i = i < 10 ? "0" i : i;
s = s < 10 ? "0" s : s;
return yearToDay(time) ' ' h ':' i ':' s;
};
//2022-03-31 10:15:20
function yearToZero(){
return yearToDay(time) " 00:00:00"
}
//2022-03-31 00:00:00
let curDate = new Date()
var preDate = new Date(curDate.getTime() - 24*60*60*1000); //前一天
var nextDate = new Date(curDate.getTime() 24*60*60*1000);//後一天
console.log(yearToSecond(preDate))//前一天
console.log(yearToSecond(nextDate))//後一天
//2022-03-30 10:15:20
//2022-04-01 10:15:20
function getCurDay(){
let nowdays = new Date(),
year = nowdays.getFullYear(),
month = nowdays.getMonth() 1;
month = month > 9 ? month : "0" month;
let firstDayOfCurMonth = `${year}-${month}-01`,
lastDay = new Date(year, month, 0),
lastDayOfCurMonth = `${year}-${month}-${lastDay.getDate()}`;
return [firstDayOfCurMonth,lastDayOfCurMonth]
};
//['2022-03-01','2022-03-31']
function getPreDay() {
let nowdays = new Date(),
year = nowdays.getFullYear(),
month = nowdays.getMonth();
if (month == 0) {
month = 12;
year = year - 1;
}
month = month > 9 ? month : "0" month;
let firstDayOfPreMonth = `${year}-${month}-01`,
lastDay = new Date(year, month, 0),
lastDayOfPreMonth = `${year}-${month}-${lastDay.getDate()}`
return [firstDayOfPreMonth,lastDayOfPreMonth]
};
//['2022-02-01', '2022-02-28']
function getLast3Month() {
var now = new Date(),
year = now.getFullYear(),
month = now.getMonth() 1,
day = now.getDate(),
dateObj = {};
month = month > 9 ? month : "0" month;
day = day > 9 ? day : "0" day;
dateObj.now = year '-' month '-' day;
if (parseInt(month) == 1) {//如果是1月份,則取上一年的10月份
dateObj.last = (parseInt(year) - 1) '-10-' day;
return dateObj;
}
if (parseInt(month) == 2) {//如果是2月份,則取上一年的11月份
dateObj.last = (parseInt(year) - 1) '-11-' day;
return dateObj;
}
if (parseInt(month) == 3) {//如果是3月份,則取上一年的12月份
dateObj.last = (parseInt(year) - 1) '-12-' day;
return dateObj;
}
var preSize = new Date(year, parseInt(month) - 3, 0).getDate();//開始時間所在月的總天數
if (preSize < parseInt(day)) {
// 開始時間所在月的總天數<本月總天數,比如當前是5月30日,在2月中沒有30,則取下個月的第一天(3月1日)為開始時間
let resultMonth = parseInt(month) - 2 < 10 ? ('0' parseInt(month) - 2) : (parseInt(month) - 2);
dateObj.last = year '-' resultMonth '-01';
return dateObj;
}
if (parseInt(month) <= 10) {
dateObj.last = year '-0' (parseInt(month) - 3) '-' day;
return dateObj;
} else {
dateObj.last = year '-' (parseInt(month) - 3) '-' day;
return dateObj;
}
};
//{last: "2021-12-31",now: "2022-03-31" }
沒了,結束了,是不是很簡單呐,如有問題,歡迎留言。如果此篇博文對您有幫助,還請動動小手點點關注 點點贊 收藏 ⭐留言呐~,謝謝 ~ ~
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!