How to convert numbers to time in JavaScript
Jul 01, 2021 am 11:10 AMJavaScript将数字转换成时间的方法:首先把字符串进行日期的封装;然后封装转换函数;最后调用函数“date.Format("yyyy-MM-dd hh:mm:ss");”即可。
本文操作环境:windows7系统、javascript1.8.5版,DELL G3电脑
JavaScript怎么将数字转换成时间?
js将 一串数字1403149534转换为日期格式
js中接收到后台返回的json字符串中的日期类型的字段都变成了一串数字,形如:1500341149000。所以我们需要将这个串格式化形如:2017-07-18 09:25:49.
直接上代码:
1、先把字符串进行日期的封装 var date = new Date(1500341149000);
2、封装转换函数
Date.prototype.Format = function(fmt) { //author:wangweizhen 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; };
3、调用函数格式化:date.Format("yyyy-MM-dd hh:mm:ss");
这样就可以完成转换了,献给需要的朋友,这是前台格式化,那么我们是否能在转转换json之前进行一些操作,
得到我们想要的格式呢,也就是这个转换操作放在后台来做,有好的想法的读者可以交流。
推荐学习:《javascript高级教程》
The above is the detailed content of How to convert numbers to time in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

Simple JavaScript Tutorial: How to Get HTTP Status Code

How to use insertBefore in javascript

How to get HTTP status code in JavaScript the easy way
