js 关于时间日期常用格式化的整理
发布日期:2021-10-16 12:05:06
浏览次数:6
分类:技术文章
本文共 4620 字,大约阅读时间需要 15 分钟。
一、验证是否是日期
checkDate(date){ if(date.match(/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/)==null){ return false; } return true;}
二、获取当前日期 yyyy-mm-dd 格式
getFormatDate() { let date = new Date(); let month = '' + (date.getMonth() + 1); let day = '' + date.getDate(); let year = date.getFullYear(); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; return [year, month, day].join('-');}
三、yyyymmdd 转 yyyy-MM-dd 格式
getFormatDate(val){ if(val){ let time = val.toString(); return time.trim().length === 8 ? time.substring(0,4) + "-" + time.substring(4,6) + "-" + time.substring(6) : time; }}
四、获取上一个月的日期 格式为yyyy-mm-dd的日期
getPreMonth(date) { //data: yyyy-mm-dd let arr = date.split(' ')[0].split('-'); let year = arr[0]; //获取当前日期的年份 let month = arr[1]; //获取当前日期的月份 let day = arr[2]; //获取当前日期的日 let days = new Date(year, month, 0); days = days.getDate(); //获取当前日期中月的天数 let year2 = year; let month2 = parseInt(month) - 1; if (month2 === 0) { year2 = parseInt(year2) - 1; month2 = 12; } let day2 = day; let days2 = new Date(year2, month2, 0); days2 = days2.getDate(); if (day2 > days2) { day2 = days2; } if (month2 < 10) { month2 = '0' + month2; } return year2 + '-' + month2 + '-' + day2;};
五、将 Date 转化为指定格式的String(by other guys)
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); } for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt;}//调用: var time1 = new Date().Format("yyyy-MM-dd");var time2 = new Date().Format("yyyy-MM-dd hh:mm:ss");
六、将 Date 转化为指定格式的String * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)(by other guys)
/** * 对Date的扩展, 可以用 1-2 个占位符 * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) * eg: * (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S")==> 2006-07-02 08:09:04.423 * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04 * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04 * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04 * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 */ Date.prototype.pattern=function(fmt) { var o = { "M+" : this.getMonth()+1, //月份 "d+" : this.getDate(), //日 "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时 "H+" : this.getHours(), //小时 "m+" : this.getMinutes(), //分 "s+" : this.getSeconds(), //秒 "q+" : Math.floor((this.getMonth()+3)/3), //季度 "S" : this.getMilliseconds() //毫秒 }; var week = { "0" : "/u65e5", "1" : "/u4e00", "2" : "/u4e8c", "3" : "/u4e09", "4" : "/u56db", "5" : "/u4e94", "6" : "/u516d" }; if(/(y+)/.test(fmt)){ fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); } if(/(E+)/.test(fmt)){ fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]); } for(var k in o){ if(new RegExp("("+ k +")").test(fmt)){ fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); } } return fmt; } var date = new Date(); window.alert(date.pattern("yyyy-MM-dd hh:mm:ss"));
转载地址:https://blog.csdn.net/snow51/article/details/82289068 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月06日 17时31分08秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
05计算机视觉-opencv边缘检测
2019-04-26
07 计算机视觉-opencv模板匹配
2019-04-26
Word2vec与logging模块_完整代码_CodingPark编程公园
2019-04-26
Auto-Encoder&VAE_完整代码_CodingPark编程公园
2019-04-26
Debug讲解_CodingPark编程公园
2019-04-26
命名实体识别_利用CRF_CodingPark编程公园
2019-04-26
Pycharm使用-取消pytest的办法_CodingPark编程公园
2019-04-26
OpenCV图像处理基础_CodingPark编程公园
2019-04-26
生成个性二维码_CodingPark编程公园
2019-04-26
读txt文件报错_CodingPark编程公园
2019-04-26
文件读&写_Python_CodingPark编程公园
2019-04-26
list与str的转换&list嵌套问题_CodingPark编程公园
2019-04-26
Gensim库生成与导入W2V模型_CodingPark编程公园
2019-04-26
cmd命令行实用技巧_CodingPark编程公园
2019-04-26
如何在junit中使用SpringFramework的Ioc容器
2019-04-26