前言虽然有成熟好用的处理日期的库,比如(momentjs和date-fns),但是在实际开发中,我们有时候可能不需要整个库.所以我在下面整理了前端开发中对日期时间的各种操作,比较全面。一部分来自我自己,一部分来自我们万能的网友~获取当前时间戳vartimestamp=Date.parse(newDate());//精确到秒vartimestamp=(newDate()).valueOf();//精确到毫秒vartimestamp=newDate().getTime();//精确到毫秒vartimestamp=+newDate();vartimestamp=Date.now();获取指定时间戳vartimestamp=(newDate("2019/10/2408:00:00")).getTime();vartimestamp=(newDate("2019-10-2408:00:00")).getTime();获取前一天/后一天的当前时间Timestampvartimestamp=+newDate()-24*60*60*1000;var时间戳=+newDate()+24*60*60*1000;今天零点的时间戳vartimestamp=newDate(newDate().toLocaleDateString()).getTime();今天最晚时间的时间戳23:59:59lettimestamp=newDate(newDate().toLocaleDateString()).getTime()+24*60*60*1000-1;获取当前时间n天后的时间戳/***@param{number}n天*@returns{Number}返回值为毫秒时间*/functiontoNextTimes(n){lettimestamp=+newDate()+n*86400000;returntimestamp;}本周第一天/****@return{*}WeekFirstDay返回本周第一天的时间*/functionshowWeekFirstDay(){letNowdate=newDate();让WeekFirstDay=newDate(Nowdate-(Nowdate.getDay()-1)*86400000);returnWeekFirstDay;}本周最后一天/****@return{*}WeekLastDay返回本周最后一天的时间*/functionshowWeekLastDay(){letNowdate=newDate();让WeekFirstDay=newDate(Nowdate-(Nowdate.getDay()-1)*86400000);让WeekLastDay=新日期((WeekFirstDay/1000+6*86400)*1000);returnWeekLastDay;}本月第一天/****@return{*}MonthFirstDay返回本月第一天的时间*/functionshowMonthFirstDay(){letNowdate=newDate();让MonthFirstDay=newDate(Nowdate.getFullYear(),Nowdate.getMonth());returnMonthFirstDay;}月末日/****@return{*}MonthLastDay返回月末日时间*/functionshowMonthLastDay(){letNowdate=newDate();让MonthNextFirstDay=newDate(Nowdate.getFullYear(),Nowdate.getMonth()+1);让MonthLastDay=新日期(MonthNextFirstDay-86400000);returnMonthLastDay;}datetotimestamp/***@param{String}时间-日期字符串,如'2018-8-8','2018,8,8','2018/8/8'*@returns{Number}返回值是以毫秒为单位的时间*/functiontimeToTimestamp(time){letdate=newDate(time);让时间戳=date.getTime();returntimestamp;}格式化当前时间/****@return{string}timeText返回系统时间字符串*/functiongetdataTimeSec(){lettime=newDate();让weekDay;让年份=time.getFullYear();让月=time.getMonth()+1;让day=time.getDate();//获取时分秒leth=time.getHours();让m=time.getMinutes();让s=time.getSeconds();//检查是否小于10h=check(h);m=检查(m);s=支票;让now_day=time.getDay();switch(now_day){case0:{weekDay="Sunday"}休息;案例1:{weekDay="星期一"}休息;案例2:{weekDay="Tuesday"}break;案例3:{weekDay="Wednesday"}break;案例4:{weekDay="Thursday"}break;案例5:{weekDay="Friday"}break;案例6:{weekDay="week6"}break;case7:{weekDay="Sunday"}break;}让timeText=year+"year"+month+"month"+day+"day"+""+weekDay+""+h+":"+m+":"+s;returntimeText}返回指定时间戳之间的时间间隔/***@param{*}startTime开始时间的时间戳*@param{*}endTime结束时间的时间戳*@return{string}str返回时间string*/functiongetTimeInterval(startTime,endTime){letrunTime=parseInt((endTime-startTime)/1000);letyear=Math.floor(runTime/86400/365);runTime=runTime%(86400*365);让月=Math.floor(runTime/86400/30);runTime=runTime%(86400*30);让天=Math.floor(runTime/86400);runTime=runTime%86400;让小时=Math.floor(runTime/3600);runTime=runTime%3600;letminute=Math.floor(runTime/60);runTime=runTime%60;letsecond=runTime;letstr='';如果(year>0){str=year+'year';}if(year<=0&&month>0){str=月+'月';}if(year<=0&&month<=0&&day>0){str=day+'day';}if(year<=0&&month<=0&&day<=0&&hour>0){str=hour+'hour';}if(year<=0&&month<=0&&day<=0&&hour<=0&&minute>0){str=minute+'minute';}if(year<=0&&month<=0&&day<=0&&hour<=0&&minute<=0&&second>0){str+=second+'second';}str+='之前';returnstr;}按类型格式化日期/***@param{*}date具体日期变量*@param{string}dateType需要返回类型*@return{string}dateText返回指定格式的日期字符串*/functiongetFormatDate(date,dateType){让dateObj=newDate(date);让月=dateObj.getMonth()+1;让strDate=dateObj.getDate();让hours=dateObj.getHours();让分钟=dateObj.getMi坚果();让秒=dateObj.getSeconds();如果(月>=1&&月<=9){月="0"+月;}if(strDate>=0&&strDate<=9){strDate="0"+strDate;}if(hours>=0&&hours<=9){hours="0"+hours}if(minutes>=0&&minutes<=9){minutes="0"+minutes}if(seconds>=0&&seconds<=9){seconds="0"+seconds}letdateText=dateObj.getFullYear()+'年'+(dateObj.getMonth()+1)+'月'+dateObj.getDate()+'日';如果(dateType=="yyyy-mm-dd"){dateText=dateObj.getFullYear()+'-'+(dateObj.getMonth()+1)+'-'+dateObj.getDate();}if(dateType=="yyyy.mm.dd"){dateText=dateObj.getFullYear()+'.'+(dateObj.getMonth()+1)+'.'+dateObj.getDate();}if(dateType=="yyyy-mm-ddMM:mm:ss"){dateText=dateObj.getFullYear()+'-'+month+'-'+strDate+''+小时+":"+分钟+":"+秒;}if(dateType=="mm-ddMM:mm:ss"){dateText=month+'-'+strDate+''+hours+":"+minutes+":"+seconds;}if(dateType=="yyyymmmonthdddayMM:mm:ss"){dateText=dateObj.getFullYear()+'Year'+month+'month'+strDate+'day'+''+hours+":"+分钟+":"+秒;}返回日期文本;}判断是否是闰年/***@param{number}year要判断的年份*@return{boolean}返回一个布尔值*/functionleapYear(year){return!(year%(year%100?4:400));}返回两年之间的闰年/***@param{number}startstartyear*@param{number}endendyear*@return{array}arr返回闰年数组*/functionleapYears(start,end){让arr=[];for(vari=start;i
