tft每日頭條

 > 生活

 > js獲取連續10天的日期

js獲取連續10天的日期

生活 更新时间:2024-08-01 04:17:27

js獲取連續10天的日期(js獲取當前時間年月日時分秒)1

年月日

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

年月日,時分秒補0

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每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved