.getMonth()
.getMonth()
は 0 から始まる数値を返すため、正しい月を取得するには 1 を追加する必要があり4
ます5
。
したがって、コードでcurrentdate.getMonth()+1
正しい値を出力するために使用できます。加えて:
.getDate()
月の日を返します<-これはあなたが望むものです
.getDay()
Date
現在の曜日 (0-6)0 == Sunday
などを表す整数を返すオブジェクトの別のメソッドです。
したがって、コードは次のようになります。
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
JavaScript Date インスタンスは Date.prototype から継承します。コンストラクターのプロトタイプ オブジェクトを変更して、JavaScript の Date インスタンスによって継承されるプロパティとメソッドに影響を与えることができます。
プロトタイプ オブジェクトを利用して、Date
今日の日付と時刻を返す新しいメソッドを作成できます。これらの新しいメソッドまたはプロパティは、Date
オブジェクトのすべてのインスタンスに継承されるため、この機能を再利用する必要がある場合に特に役立ちます。
// For todays date;
Date.prototype.today = function () {
return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear();
}
// For the time now
Date.prototype.timeNow = function () {
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}
次に、次のようにして日付と時刻を簡単に取得できます。
var newDate = new Date();
var datetime = "LastSync: " + newDate.today() + " @ " + newDate.timeNow();
または、メソッドをインラインで呼び出して、単純に-
var datetime = "LastSync: " + new Date().today() + " @ " + new Date().timeNow();