.getMonth()
returns a zero-based number so to get the correct month you need to add 1, so calling .getMonth()
in may will return 4
and not 5
.
(.getMonth()
返回一个从零开始的数字,以便获得需要添加1的正确月份,因此调用.getMonth()
可能会返回4
而不是5
。)
So in your code we can use currentdate.getMonth()+1
to output the correct value.
(因此,在您的代码中,我们可以使用currentdate.getMonth()+1
来输出正确的值。)
In addition:(此外:)
so your code should look like this:
(所以你的代码应该是这样的:)
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
JavaScript Date instances inherit from Date.prototype.
(JavaScript Date实例继承自Date.prototype。)
You can modify the constructor's prototype object to affect properties and methods inherited by JavaScript Date instances(您可以修改构造函数的原型对象,以影响JavaScript Date实例继承的属性和方法)
You can make use of the Date
prototype object to create a new method which will return today's date and time.
(您可以使用Date
原型对象来创建一个新方法,该方法将返回今天的日期和时间。)
These new methods or properties will be inherited by all instances of the Date
object thus making it especially useful if you need to re-use this functionality.(这些新方法或属性将由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();
}
You can then simply retrieve the date and time by doing the following:
(然后,您可以通过执行以下操作来简单地检索日期和时间:)
var newDate = new Date();
var datetime = "LastSync: " + newDate.today() + " @ " + newDate.timeNow();
Or call the method inline so it would simply be -
(或者调用内联方法,这样就可以了 - )
var datetime = "LastSync: " + new Date().today() + " @ " + new Date().timeNow();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…