JS Code block 代码块–日期
星期一, 2023-04-17 | Author: Lee | computer, 前端 | 442 views
日期
按类型格式化日期
设置几天后的日期
获取当前时间的n天后的时间戳
本周第一天
本周最后一天
本月第一天
本月最后一天
日期转时间戳
返回指定时间戳之间的时间间隔
星期转换,将数字转换成英文
月份转换,将数字转换成英文
是否为闰年
返回两个年份之间的闰年
/** * 按类型格式化日期 * @param {*} date 具体日期变量 * @param {string} dateType 需要返回类型,包括('yyyy年mm月dd日','yyyy-mm-dd', 'yyyy.mm.dd','yyyy-mm-dd MM:mm:ss', 'mm-dd MM:mm:ss', 'yyyy年mm月dd日 MM:mm:ss') * @return {string} dateText 返回为指定格式的日期字符串 */ function getFormatDate(date, dateType) { let dateObj = new Date(date); let month = dateObj.getMonth() + 1; let strDate = dateObj.getDate(); let hours = dateObj.getHours(); let minutes = dateObj.getMinutes(); let seconds = dateObj.getSeconds(); if (month >= 1 && month <= 9) { month = "0" + month; } 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 } let 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.dd") { dateText = dateObj.getFullYear() + '.' + (dateObj.getMonth() + 1) + '.' + dateObj.getDate(); } if (dateType == "yyyy-mm-dd MM:mm:ss") { dateText = dateObj.getFullYear() + '-' + month + '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds; } if (dateType == "mm-dd MM:mm:ss") { dateText = month + '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds; } if (dateType == "yyyy年mm月dd日 MM:mm:ss") { dateText = dateObj.getFullYear() + '年' + month + '月' + strDate + '日' + ' ' + hours + ":" + minutes + ":" + seconds; } return dateText; } /** * 设置几天后的日期 * @param {string} date 起始日期 * @param {number} day 向后的天数 * @return {string} 返回想要得到的日期 */ function convertDate (date, day) { let tempDate = new Date(date); tempDate.setDate(tempDate.getDate()+day); let Y = tempDate.getFullYear(); let M = tempDate.getMonth()+1 < 10 ? '0'+(tempDate.getMonth()+1) : tempDate.getMonth()+1; let D = tempDate.getDate() < 10 ? '0'+(tempDate.getDate()) : tempDate.getDate(); let result = Y + "-" + M + "-" + D return result; } /** * 获取当前时间的n天后的时间戳 * @param {number} n 天数 * @returns {Number} 返回值为时间毫秒值 */ function toNextTimes(n){ let timestamp = +new Date() + n * 86400000; return timestamp; } /*** * 本周第一天 * @return {*} WeekFirstDay 返回本周第一天的时间 */ function showWeekFirstDay(){ let Nowdate = new Date(); let WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000); return WeekFirstDay; } /*** * 本周最后一天 * @return {*} WeekLastDay 返回本周最后一天的时间 */ function showWeekLastDay(){ let nowDate = new Date(); let weekFirstDay=new Date(nowDate-(nowDate.getDay()-1)*86400000); let WeekLastDay=new Date((weekFirstDay/1000+6*86400)*1000); return WeekLastDay; } /*** * 本月第一天 * @return {*} MonthFirstDay 返回本月第一天的时间 */ function showMonthFirstDay(){ let Nowdate=new Date(); let MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()); return MonthFirstDay; } /*** * 本月最后一天 * @return {*} MonthLastDay 返回本月最后一天的时间 */ function showMonthLastDay(){ let Nowdate=new Date(); let MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()+1); let MonthLastDay=new Date(MonthNextFirstDay-86400000); return MonthLastDay; } /** * 日期转时间戳 * @param {String} time - 日期字符串,如'2018-8-8','2018,8,8','2018/8/8' * @returns {Number} 返回值为时间毫秒值 */ function timeToTimestamp (time) { let date = new Date(time); let timestamp = date.getTime(); return timestamp; } /** * 返回指定时间戳之间的时间间隔 * @param {*} startTime 开始时间的时间戳 * @param {*} endTime 结束时间的时间戳 * @return {string} str 返回时间字符串 格式:yyyy年mm月dd日MM小时mm分钟ss秒前 */ function getTimeInterval(startTime, endTime) { let runTime = parseInt((endTime - startTime) / 1000); let year = Math.floor(runTime / 86400 / 365); runTime = runTime % (86400 * 365); let month = Math.floor(runTime / 86400 / 30); runTime = runTime % (86400 * 30); let day = Math.floor(runTime / 86400); runTime = runTime % 86400; let hour = Math.floor(runTime / 3600); runTime = runTime % 3600; let minute = Math.floor(runTime / 60); runTime = runTime % 60; let second = runTime; let str = ''; if (year > 0) { str = year + '年'; } if (year <= 0 && month > 0) { str = month + '月'; } if (year <= 0 && month <= 0 && day > 0) { str = day + '天'; } if (year <= 0 && month <= 0 && day <= 0 && hour > 0) { str = hour + '小时'; } if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute > 0) { str = minute + '分钟'; } if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute <= 0 && second > 0) { str += second + '秒'; } str += '前'; return str; } /** * 星期转换,将数字转换成英文 0为sunday * @param num * @returns {null} */ function weekNumToStr(num) { let week = null; switch (num) { case 1 : { week = 'MONDAY'; break; } case 2 : { week = "TUESDAY"; break; } case 3 : { week = "WEDNESDAY"; break; } case 4 : { week = "THURSDAY"; break; } case 5 : { week = "FRIDAY"; break; } case 6 : { week = "SATURDAY"; break; } case 0 : { week = "SUNDAY"; break; } } return week; } /** * 月份转换,将数字转换成英文 0为January * @param num * @returns {null} */ function monthNumToStr(num) { let month = null; switch (num) { case 0 : { month = 'JANUARY'; break; } case 1 : { month = "FEBRUARY"; break; } case 2 : { month = "MARCH"; break; } case 3 : { month = "APRIL"; break; } case 4 : { month = "MAY"; break; } case 5 : { month = "JUNE"; break; } case 6 : { month = "JULY"; break; } case 7 : { month = "AUGUST"; break; } case 8 : { month = "SEPTEMBER"; break; } case 9 : { month = "OCTOBER"; break; } case 10 : { month = "NOVEMBER"; break; } case 11 : { month = "DECEMBER"; break; } } return month; } /** * 判断是否为闰年 * @param {number} year 要判断的年份 * @return {boolean} 返回布尔值 */ export function leapYear(year) { return !(year % (year % 100 ? 4 : 400)); } /** * 返回两个年份之间的闰年 * @param {number} start 开始年份 * @param {number} end 结束年份 * @return {array} arr 返回符合闰年的数组 */ export function leapYears(start, end) { let arr = []; for (var i=start; i<end; i++) { if ( leapYear(i) ) { arr.push(i) } } return arr } |
文章作者: Lee
本文地址: https://www.pomelolee.com/2369.html
除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址
No comments yet.
Leave a comment
Search
相关文章
热门文章
最新文章
文章分类
- ajax (10)
- algorithm-learn (3)
- Android (6)
- as (3)
- computer (85)
- Database (30)
- disucz (4)
- enterprise (1)
- erlang (2)
- flash (5)
- golang (3)
- html5 (18)
- ios (4)
- JAVA-and-J2EE (186)
- linux (143)
- mac (10)
- movie-music (11)
- pagemaker (36)
- php (50)
- spring-boot (2)
- Synology群晖 (2)
- Uncategorized (6)
- unity (1)
- webgame (15)
- wordpress (33)
- work-other (2)
- 低代码 (1)
- 体味生活 (40)
- 前端 (21)
- 大数据 (8)
- 游戏开发 (9)
- 爱上海 (19)
- 读书 (4)
- 软件 (3)