Поскольку часто необходимо форматировать даты в работе, в отличие от бэкэнда, есть удобные методы для вызова, и вы можете определить метод формата в прототипе объекта даты, см. В следующем:
Кода -копия выглядит следующим образом:
// Добавить метод форматирования для прототипа даты и времени
Date.prototype.format = function (formatstr) {
var str = formatstr;
var week = ['day', 'one', 'two', 'three', 'four', 'five', 'six'];
str = str.Replace (/yyyy | yyyy/, this.getbleear ());
str = str.replace (/yy | yy/, (this.getyear () % 100)> 9? (this.getyear () % 100) .toString (): '0' + (this.getyear () % 100));
var month = this.getmonth () + 1;
str = str.replace (/мм/, месяц> 9? Месяц.toString (): '0' + месяц);
str = str.replace (/м/г, месяц);
str = str.replace (/w | w/g, неделя [this.getday ()]);
str = str.replace (/dd | dd/, this.getDate ()> 9? this.getDate (). toString (): '0' + this.getDate ());
str = str.replace (/d | d/g, this.getDate ());
str = str.replace (/hh | hh/, this.gethours ()> 9? this.gethours (). toString (): '0' + this.gethours ());
str = str.replace (/h | h/g, this.gethours ());
str = str.replace (/mm/, this.getminutes ()> 9? this.getMinutes (). ToString (): '0' + this.getMinutes ());
str = str.replace (/m/g, this.getminutes ());
str = str.replace (/ss | ss/, this.getSeconds ()> 9? this.getSeconds (). ToString (): '0' + this.getSeconds ());
str = str.replace (/s | s/g, this.getSeconds ());
вернуть Str;
}
Например, это относительно просто:
Кода -копия выглядит следующим образом:
var d = new Date ();
var str = d.format ("yyyy-mm-dd hh: mm: ss");
console.log (str);