当前位置: 首页 > Web前端 > HTML

JS日期格式;获取最近7天、最近半年、最近一年的日期(React,ReactNative)

时间:2023-03-28 14:19:20 HTML

文档:JS日期对象Date.prototype.Format=function(fmt){leto={"M+":this.getMonth()+1,//month"d+":this.getDate(),//day"H+":this.getHours(),//小时"m+":this.getMinutes(),//分"s+":this.getSeconds(),//秒"q+":Math.floor((this.getMonth()+3)/3),//四分之一“S”:this.getMilliseconds()//毫秒};如果(/(y+)/.test(fmt))fmt=fmt.replace(RegExp.$1,(this.getFullYear()+"").substring(4-RegExp.$1.length));for(letkino){if(newRegExp("("+k+")").test(fmt)){fmt=fmt.replace(RegExp.$1,(RegExp.$1.length==1)?(o[k]):(("00"+o[k]).substring((""+o[k]).length)));}}returnfmt;}/***@description:日期格式转换*@param{*}*@return{*}*@param{*}datenewDate()parameters:*newDate();无*新日期(值);Unix时间戳:1652054400000*newDate(date细绳);时间戳字符串:'2020-05-09'*newDate(year,monthIndex[,day[,hours[,minutes[,seconds[,milliseconds]]]]]);分别提供日期和时间每个成员*@param{*}Type日期格式(以英文为例,区分字母和小写)E.G"YyyyYearMMMM:MM:SS"*/Exstthtformatdate=(Date,Type)=>>{让curDate=日期||newDate()letnewDate=newDate(curDate).Format(type)returnnewDate}/***@description:获取n年前的指定日期*@param{*}n*@return{*}Unix时间戳例如:1652054400000*/exportconstgetYearAgoDate=(n=0)=>{letcurDate=newDate()curDate.setFullYear(curDate.getFullYear()-n)constts=+curDatereturnts}/***@description:获取n个月前的指定日期*@param{*}n*@return{*}Unix时间戳e.g:1652054400000*/exportconstgetMonthAgoDate=(n=0)=>{letcurDate=newDate()curDate.setMonth(curDate.getMonth()-n)constts=+curDatereturnts}/***@description:获取n天前的指定日期*@param{*}n*@return{*}Unix时间戳e.g:1652054400000*/exportconstgetDayAgoDate=(n=0)=>{让curDate=newDate();让newDate=newDate(curDate-1000*60*60*24*n);constts=+newDate返回ts}