在JavaScript中获取当前日期和时间 [英] Getting current date and time in JavaScript
问题描述
DATE
是错误的。这是代码: var currentdate = new Date();
var datetime =Last Sync:+ currentdate.getDay()+/+ currentdate.getMonth()
+/+ currentdate.getFullYear()+@
+ currentdate.getHours()+:
+ currentdate.getMinutes()+:+ currentdate.getSeconds();
应该打印 18/04/2012 15:07:33
并打印 3/3/2012 15:07:33
任何帮助?感谢
调用 .getMonth()
时,需要添加+ 1显示正确的月份。
JavaScript计数始终从0开始(请看这里来检查原因),因此调用 .getMonth()
可能会返回 4
而不是 5
。
所以在你的代码中,我们可以使用 currentdate.getMonth()+ 1
来输出正确的值。另外:
-
.getDate()
返回本月的日子< - 这是你想要的 -
.getDay()
是一个单独的Date
对象,它将返回一个整数,表示当前星期几(0-6)0 ==星期日
等 -
所以你的代码应该如下所示:
var currentdate = new Date();
var datetime =Last Sync:+ currentdate.getDate()+/
+(currentdate.getMonth()+ 1)+/
+ currentdate.getFullYear +@
+ currentdate.getHours()+:
+ currentdate.getMinutes()+:
+ currentdate.getSeconds();
JavaScript日期实例继承自Date.prototype。您可以修改构造函数的原型对象以影响JavaScript Date实例继承的属性和方法
可以使用原型构造函数 Date
对象,并创建一个新的 Date方法返回今天的日期和时间。这些新的方法或属性将被
Date
对象的所有实例继承,因此如果您需要重新使用此功能,则特别有用。
//今天的日期;
Date.prototype.today = function(){
return((this.getDate()< 10)?0:)+ this.getDate()+/+ ((this.getMonth()+ 1)< 10)?0:)+(this.getMonth()+ 1)+/+ this.getFullYear();
}
//现在的时间
Date.prototype.timeNow = function(){
return((this.getHours()< 10)? 0:)+ this.getHours()+:+((this.getMinutes()< 10)?0:)+ this.getMinutes()+:+ this.getSeconds()< 10)?0:)+ this.getSeconds();
}
然后,您可以通过执行以下操作简单地检索日期和时间: p>
var newDate = new Date();
var datetime =LastSync:+ newDate.today()+@+ newDate.timeNow();
或者直接调用方法,这样只会是 -
var datetime =LastSync:+ new Date()。today()+@+ new Date()。
I have a script that prints the current date and time in JavaScript, but the DATE
is allways wrong. Here is the code:
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDay() + "/"+currentdate.getMonth()
+ "/" + currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":" + currentdate.getSeconds();
It should print 18/04/2012 15:07:33
and prints 3/3/2012 15:07:33
Any help? Thanks
When calling .getMonth()
you need to add +1 to display the correct month.
Javascript count always starts at 0 (look here to check why), so calling .getMonth()
in may will return 4
and not 5
.
So in your code we can use currentdate.getMonth()+1
to output the correct value. In addition:
.getDate()
returns the day of the month <- this is the one you want.getDay()
is a separate method of theDate
object which will return an integer representing the current day of the week (0-6)0 == Sunday
etc
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. You can modify the constructor's prototype object to affect properties and methods inherited by JavaScript Date instances
You can make use of the prototype constructor for the Date
object and create a new method of the Date
object to return today's date and time. 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.
// 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();
这篇关于在JavaScript中获取当前日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!