首頁  >  文章  >  web前端  >  js以指定格式顯示日期時間的樣式程式碼_javascript技巧

js以指定格式顯示日期時間的樣式程式碼_javascript技巧

WBOY
WBOY原創
2016-05-16 17:37:18985瀏覽

[Ctrl A 全選注:如需引入外部Js需刷新才能執行
]

時間為當前時間 代碼如下:


//1.2013-4-9 11:21:32
//2.2013年4月9日11點21分32秒
//3.2013年4月9日上午11點21分32秒
//4.2013年4月9日下午13點21分32秒
/*
var thedate = new Date();
alert(thedate.getFullYear() '-' thedate.getMonth() '-' thedate.getDate() ' ' thedate.toLocaleTimeString() );
alert(thedate.toLocaleDateString() ' ' thedate.getHours() '點' thedate.getMinutes() '分' thedate.getSeconds() '秒');
if (thedate.getHours() alert(thedate.toLocaleDateString() ' 上午' thedate.getHours() '點' thedate.getMinutes() '分' thedate.getSeconds() '秒');
}
else {
alert(thedate.toLocaleDateString() ' 下午' thedate.getHours() '點' thedate.getMinutes() '分' thedate.getSeconds() '秒');
}
*/
<script> /// <summary> /// 格式化显示日期时间 /// /// <param name="x">待显示的日期时间,例如new Date() /// <param name="y">需要显示的格式,例如yyyy-MM-dd hh:mm:ss 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)}); } alert(date2str(new Date(),"yyyy-MM-dd hh:mm:ss")); alert(date2str(new Date(),"yyyy-M-d h:m:s")); </script>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn