Home  >  Article  >  Web Front-end  >  js style code to display date and time in a specified format_javascript skills

js style code to display date and time in a specified format_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:37:181020browse

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

The time is the current time The code is as follows:


//1.2013-4-9 11:21:32
//2.April 9, 2013 11:21:32
//3.April 9, 2013 11:21:32 am
//4.April 9, 2013 13:21:32 pm Seconds
/*
var thedate = new Date();
alert(thedate.getFullYear() '-' thedate.getMonth() '-' thedate.getDate() ' ' thedate.toLocaleTimeString() );
alert(thedate.toLocaleDateString() ' ' thedate.getHours() 'point' thedate.getMinutes() 'minute' thedate.getSeconds() 'second');
if (thedate.getHours() < 12) {
alert(thedate.toLocaleDateString() 'am' thedate.getHours() 'point' thedate.getMinutes() 'minute' thedate.getSeconds() 'seconds');
}
else {
alert(thedate.toLocaleDateString() 'pm' thedate.getHours() 'point' thedate.getMinutes() 'minute' thedate.getSeconds() 'second');
}
* /
<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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn