本文实例总结了javascript日期格式化方法。分享给大家供大家参考,具体如下:
采用Prototype:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Date .prototype.Format = function (fmt) {
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;
}
|
Copier après la connexion
把这段代码放在<script></script>里面。
然后调用时采用如下语句即可:
new Date().Format("yyyy-MM-dd hh:mm:ss")
具体可参考前面一篇《
javascript中Date format(js日期格式化)方法小结》
另外获取当前日期的函数如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <SCRIPT LANGUAGE= "JavaScript" >
<!--
var myDate = new Date ();
myDate.getYear();
myDate.getFullYear();
myDate.getMonth();
myDate. getDate ();
myDate.getDay();
myDate.getTime();
myDate.getHours();
myDate.getMinutes();
myDate.getSeconds();
myDate.getMilliseconds();
myDate.toLocaleDateString();
var mytime=myDate.toLocaleTimeString();
myDate.toLocaleString( );
if (mytime< "23:30:00" )
{
alert(mytime);
}
</SCRIPT>
|
Copier après la connexion
希望本文所述对大家JavaScript程序设计有所帮助。