多くの場合、作業中の日付をフォーマットする必要があるため、バックエンドとは異なり、呼び出す便利な方法があり、日付オブジェクトのプロトタイプでフォーマットメソッドを定義できます。次のように参照してください。
コードコピーは次のとおりです。
//日付と時刻のプロトタイプのフォーマットメソッドを追加します
date.prototype.format = function(formattr){
var str = formattr;
var week = ['day'、 'one'、 'two'、 'three'、 'four'、 'five'、 'six'];
str = str.replace(/yyyy | yyyy/、this.gettlyear());
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(/mm/、月> 9?月.toString(): '0' +月);
str = str.replace(/m/g、月);
str = str.replace(/w | w/g、week [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);