在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才能獲得“人工”月份數。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章