您好,欢迎访问一九零五行业门户网

javascript如何转换时间

javascript转换时间的方法:创建一个【util.js】文件,在里面重新封装一下date的format方法,代码为【let date = new date().format(yyyy年m月dd日\teee\thh:mm:ss)】。
本教程操作环境:windows7系统、javascript1.8.5版,dell g3电脑。
javascript转换时间的方法:
创建一个util.js文件,在里面重新封装一下date的format方法:
//date的prototype 属性可以向对象添加属性和方法。 date.prototype.format = 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;};
具体调用的时候就可以变得很方便,想要什么格式就可以形成什么格式,例如 年月日 星期几 时分秒,做个简单的例子,如果有哪里看不明白可以评论,我看见会给你解释的。
let date = new date().format("yyyy年mm月dd日\teee\thh:mm:ss"); $("#date-now").html(date);
相关免费学习推荐:javascript学习教程
以上就是javascript如何转换时间的详细内容。
其它类似信息

推荐信息