JS Date时间格式化的方法
更新时间:2024年01月20日 10:50:34 作者:请叫我彦祖
这篇文章主要介绍了JS Date时间格式化的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
JS Date时间格式化
1.1日期的格式化
1.1.1方法一(格式,日期))
// 入参 fmt-格式 date-日期 function dateFormat(fmt, date) { let ret; const opt = { "Y+": date.getFullYear().toString(), // 年 "m+": (date.getMonth() + 1).toString(), // 月 "d+": date.getDate().toString(), // 日 "H+": date.getHours().toString(), // 时 "M+": date.getMinutes().toString(), // 分 "S+": date.getSeconds().toString() // 秒 // 有其他格式化字符需求可以继续添加,必须转化成字符串 }; for (let k in opt) { ret = new RegExp("(" + k + ")").exec(fmt); if (ret) { fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0"))) }; }; return fmt; }
举个例子:
let date = new Date() dateFormat("YYYY-mm-dd HH:MM:SS", date) console.log(dateFormat("YYYY-mm-dd HH:MM", date)); 输出: 2019-06-06 19:45:35
1.1.2方法二(当前日期)
// 入参 fmt-格式 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 now = new Date(); var nowStr = now.format("yyyy-MM-dd hh:mm:ss"); //2021-06-03 19:17:35 console.log(nowStr); //2021年06月03日 console.log(new Date().format("yyyy年MM月dd日")); var nowStr = now.format("yyyy-MM-dd hh:mm:ss"); //2021-06-03 19:17:35 console.log(nowStr);// //2021年06月03日19小时17分35秒 console.log(new Date().format("yyyy年MM月dd日hh小时mm分ss秒")); //输出: 2021-06-03 19:17:35 2021年06月03日 2021-06-03 19:17:35 2021年06月03日19小时17分35秒
推荐时间插件库:moment.js
Js Date日期格式和字符串的相互转化
// 日期格式化 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; } let time = new Date().format("yyyy-MM-dd hh:mm:ss");
到此这篇关于JS Date时间格式化的文章就介绍到这了,更多相关JS Date时间格式化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
mockjs,json-server一起搭建前端通用的数据模拟框架教程
下面小编就为大家分享一篇mockjs,json-server一起搭建前端通用的数据模拟框架教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2017-12-12JS window对象的top、parent、opener含义介绍
本文为大家介绍下JS window对象的top、parent、opener含义,不了解的朋友可以参考下,希望对大家有所帮助2013-12-12JS、jquery实现几分钟前、几小时前、几天前等时间差显示效果的代码实例分享
在新浪微博首页看到每条微博后边显示的时间并不是标准的年-月-日格式,而是经过换算的时间差,如:发表于5分钟前、发表于“2小时前”,比起标准的时间显示格式,貌似更加直观和人性化2014-04-04
最新评论