JS常用时间处理方法
2020-01-14
简便的格式化时间
1 2 3 4 5 6 var format = function (time ) { var date = new Date (+time + 8 * 3600 * 1000 ); return date.toJSON().substr(0 , 19 ).replace("T" , " " ).replace(/-/g , "." ); }; var time1 = format(new Date ());
Date 的 toJSON 方法返回格林威治时间的 JSON 格式字符串,转化为北京时间需要额外增加 8 个时区,然后把 T 替换为空格,即是我们需要的时间格式,后面可以通过正则将日期分隔符换成任何想要的字符。一元加操作符可以把任何数据类型转换成数字,所以获取时间对象对应毫秒数的另一个方法是+Date 或 Number(Date)
获取当月最后一天 一个月可能有 28/29/30/31 天,使用写死数据的方式来解决闰年和大小月显然是不科学的。
1 2 3 4 5 6 7 8 9 function getLastDayOfMonth (time ) { var month = time.getMonth(); time.setMonth(month + 1 ); time.setDate(0 ); return time.getDate(); } getLastDayOfMonth(new Date ());
获取这个季度的第一天 用来确定当前季度的开始时间,常用在报表中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 function getFirstDayOfSeason (time ) { var month = time.getMonth(); if (month < 3 ) { time.setMonth(0 ); } else if (2 < month && month < 6 ) { time.setMonth(3 ); } else if (5 < month && month < 9 ) { time.setMonth(6 ); } else if (8 < month && month < 11 ) { date.setMonth(9 ); } time.setDate(1 ); return time; } getFirstDayOfSeason(new Date ());
获取中文星期 完全没必要写一长串 switch 啦,直接用 charAt 来解决。
1 let time = "日一二三四五六" .charAt(new Date ().getDay());
获取今天是当年第几天
1 2 3 4 5 6 7 var time1 = Math .ceil( (new Date () - new Date (new Date ().getFullYear().toString())) / (24 * 60 * 60 * 1000 ) );
获取今天是当年的第几周 日历、表单常用
1 2 3 4 5 6 7 var week = Math .ceil( (new Date () - new Date (new Date ().getFullYear().toString())) / (24 * 60 * 60 * 1000 ) / 7 );
获取今天是当年还剩多少天
1 2 3 4 5 6 7 8 9 function restOfYear (time ) { var nextyear = (time.getFullYear() + 1 ).toString(); var lastday = new Date (new Date (nextyear) - 1 ); console .log(lastday); var diff = lastday - time; return Math .floor(diff / (1000 * 60 * 60 * 24 )); } restOfYear(new Date ());
计算两个时间的间隔 可以拓展为倒计时、有效期等用途
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 function diffTime (startDate, endDate, location ) { var diff = new Date (endDate).getTime() - startDate; var days = Math .floor(diff / (24 * 3600 * 1000 )); var leave1 = diff % (24 * 3600 * 1000 ); var hours = Math .floor(leave1 / (3600 * 1000 )); var leave2 = leave1 % (3600 * 1000 ); var minutes = Math .floor(leave2 / (60 * 1000 )); var leave3 = leave2 % (60 * 1000 ); var seconds = Math .round(leave3 / 1000 ); if (location === "Day" ) { return (returnStr = "还有" + days + "天" ); } else if (location === "Hours" ) { return (returnStr = "还有" + (hours + days * 24 ) + "小时" ); } else if (location === "Minutes" ) { return (returnStr = "还有" + (minutes + (hours + days * 24 ) * 60 ) + "分钟" ); } else if (location === "Seconds" ) { return (returnStr = "还有" + (seconds + (minutes + (hours + days * 24 ) * 60 ) * 60 ) + "秒" ); } else { return (returnStr = "还有" + days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒" ); } } console .log(diffTime(new Date (), "2019-8-19 16:00:00" , "Minutes" ));
计算指定时间间隔前后的日期 可用于时间追溯、活动预告等用途
1 2 3 4 5 6 7 function GetDate (time, count ) { time.setDate(time.getDate() + count); var date = new Date (+time + 8 * 3600 * 1000 ); return date.toJSON().substr(0 , 19 ).replace("T" , " " ).replace(/-/g , "." ); } GetDate(new Date (), 100 );
计算当周开始和结束时间 很常见的需求,可用来做签到等
1 2 3 4 5 6 7 8 9 10 11 12 function getwholetWeek (now ) { var weekday = now.getDay(); weekday = weekday === 0 ? 7 : weekday; var firstDay = GetDate(now, -weekday); var lastDay = GetDate(now, 7 - 1 ); return { firstDay: firstDay, lastDay: lastDay, }; } console .log(getwholetWeek(new Date ()));