在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 谢谢


#1楼

参考:https://stackoom.com/question/gqNt/在JavaScript中获取当前日期和时间


#2楼

var currentdate = new Date();

    var datetime = "Last Sync: " + currentdate.getDate() + "/"+(currentdate.getMonth()+1) 
    + "/" + currentdate.getFullYear() + " @ " 
    + currentdate.getHours() + ":" 
    + currentdate.getMinutes() + ":" + currentdate.getSeconds();

Change .getDay() method to .GetDate() and add one to month, because it counts months from 0. .getDay()方法更改为.GetDate()并添加一个月,因为它从0开始计算月数。


#3楼

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

#4楼

getDay() gets the day of the week. getDay()获取星期几。 3 is Wednesday. 3是星期三。 You want getDate() , that will return 18 . 你想要getDate() ,它将返回18

Also getMonth() starts at 0 , you need to add 1 to get 4 (April). getMonth()0开始,你需要加1才能获得4 (4月)。

DEMO: http://jsfiddle.net/4zVxp/ 演示: http//jsfiddle.net/4zVxp/


#5楼

This should do the trick: 这应该做的伎俩:

function dateToString(date) {
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var dateOfString = (("" + day).length < 2 ? "0" : "") + day + "/";
    dateOfString += (("" + month).length < 2 ? "0" : "") + month + "/";
    dateOfString += date.getFullYear();
    return dateOfString;
}

var currentdate = new Date();
var datetime = "Last Sync: ";
datetime += dateToString(currentdate );
datetime += + currentdate.getHours() + ":"
            + currentdate.getMinutes() + ":"
            + currentdate.getSeconds();

#6楼

.getDay returns day of week. .getDay返回星期几。 You need .getDate instead. 你需要.getDate。 .getMonth returns values from 0 to 11. You'll need to add 1 to the result to get "human" month number. .getMonth返回0到11之间的值。您需要在结果中加1才能获得“人工”月份数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值