js获取连续10天的日期(js获取当前时间年月日时分秒)

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" }

没了,结束了,是不是很简单呐,如有问题,欢迎留言。如果此篇博文对您有帮助,还请动动小手点点关注 点点赞 收藏 ⭐留言呐~,谢谢 ~ ~

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页