![So formatieren Sie die Zeit in js](https://img.php.cn/upload/article/000/000/041/5e9118e6c7a62707.jpg)
1、获取当前时间
2、获取时间中的年月日时分秒
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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( );
|
Nach dem Login kopieren
3、时间的格式化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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;
}
|
Nach dem Login kopieren
调用:
1 2 3 | var time1 = new Date ().Format( "yyyy-MM-dd" );
var time2 = new Date ().Format( "yyyy-MM-dd hh:mm:ss" );
|
Nach dem Login kopieren
相关教程推荐:js教程
Das obige ist der detaillierte Inhalt vonSo formatieren Sie die Zeit in js. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!