在 JavaScript 中获取当前日期和时间 [英] Getting current date and time in JavaScript
问题描述
我有一个用 JavaScript 打印当前日期和时间的脚本,但是 DATE
总是错误的.代码如下:
var currentdate = new Date();var datetime =上次同步:"+ currentdate.getDay() + "/";+ 当前日期.getMonth()+ "/";+ currentdate.getFullYear() + "@"+ currentdate.getHours() + ":";+ currentdate.getMinutes() + ":";+ currentdate.getSeconds();
它应该打印 18/04/2012 15:07:33
并打印 3/3/2012 15:07:33
.getMonth()
返回一个从零开始的数字,因此要获得正确的月份,您需要加 1,因此调用 .getMonth()
可能会返回 4
而不是 5
.
因此在您的代码中,我们可以使用 currentdate.getMonth()+1
来输出正确的值.另外:
.getDate()
返回月份中的日期<-这是您想要的日期.getDay()
是Date
对象的一个单独方法,它将返回一个整数,表示当前的星期几 (0-6)0== 星期日
等
所以你的代码应该是这样的:
var currentdate = new Date();var datetime = "上次同步:" + currentdate.getDate() + "/"+ (currentdate.getMonth()+1) + "/"+ currentdate.getFullYear() + " @ "+ currentdate.getHours() + ":"+ currentdate.getMinutes() + ":"+ currentdate.getSeconds();
<小时><块引用>
JavaScript Date 实例继承自 Date.prototype.您可以修改构造函数的原型对象来影响 JavaScript Date 实例继承的属性和方法
您可以使用 Date
原型对象来创建一个新方法,该方法将返回今天的日期和时间.Date
对象的所有实例都将继承这些新方法或属性,因此如果您需要重用此功能,它会特别有用.
//为今天的日期;Date.prototype.today = 函数 () {return ((this.getDate() <10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) <10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear();}//暂时Date.prototype.timeNow = 函数 () {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();
I have a script that prints the current date and time in JavaScript, but the DATE
is always 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
.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
.
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 Date
prototype object to create a new method which will 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屋!