在JavaScript中获取当前日期和时间 [英] Getting current date and time in JavaScript

查看:169
本文介绍了在JavaScript中获取当前日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,用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 DATEis 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 the Date 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆