Js时间格式化
作者:Dreamer
出处:http://www.dreamerlzy.com/blog/article/detail/js-timeformate
说明:本文版权归作者所有,欢迎转载,但未经作者同意时,请在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
参考:
无
<script> /// <summary> /// 格式化显示日期时间 /// </summary> /// <param name="x">待显示的日期时间,例如new Date()</param> /// <param name="y">需要显示的格式,例如yyyy-MM-dd hh:mm:ss</param> function date2str(x,y) { var z = {M:x.getMonth()+1,d:x.getDate(),h:x.getHours(),m:x.getMinutes(),s:x.getSeconds()}; y = y.replace(/(M+|d+|h+|m+|s+)/g,function(v) {return ((v.length>1?"0":"")+eval('z.'+v.slice(-1))).slice(-2)}); return y.replace(/(y+)/g,function(v) {return x.getFullYear().toString().slice(-v.length)}); } //test alert(date2str(new Date(),"yyyy-MM-dd hh:mm:ss")); alert(date2str(new Date(),"yyyy-M-d h:m:s")); </script>