Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

javascript - 在JavaScript中获取当前日期和时间(Getting current date and time in JavaScript)

I have a script that prints the current date and time in JavaScript, but the DATE is always wrong.

(我有一个脚本,用JavaScript打印当前的日期和时间,但DATE总是错误的。)

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

(它应该打印18/04/2012 15:07:33并打印3/3/2012 15:07:33)

Any help?

(有帮助吗?)

Thanks

(谢谢)

  ask by Ricardo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

.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:

(此外:)

  • .getDate() returns the day of the month <- this is the one you want

    (.getDate()返回月中的某一天< - 这是你想要的那一天)

  • .getDay() is a separate method of the Date object which will return an integer representing the current day of the week (0-6) 0 == Sunday etc

    (.getDay()Date对象的一个??单独方法,它将返回一个表示当前星期几(0-6) 0 == Sunday等的整数)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...