Getting current date and time in JavaScript

I have a script that prints the current date and time in JavaScript, but the DATE is 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();
    

    To get time and date you should use

        new Date().toLocaleString();
    
    >> "09/08/2014, 2:35:56 AM"
    

    To get only the date you should use

        new Date().toLocaleDateString();
    
    >> "09/08/2014"
    

    To get only the time you should use

        new Date().toLocaleTimeString();
    
    >> "2:35:56 AM"
    

    Or if you just want the time in the format hh:mm without AM/PM for US English

        new Date().toLocaleTimeString('en-US', { hour12: false, 
                                                 hour: "numeric", 
                                                 minute: "numeric"});
    >> "02:35"
    

    or for British English

        new Date().toLocaleTimeString('en-GB', { hour: "numeric", 
                                                 minute: "numeric"});
    
    >> "02:35"
    

    Read more here.


    If you want true mysql style date time use this

    2013/10/04 08:51:32

     function getDateTime() {
        var now     = new Date(); 
        var year    = now.getFullYear();
        var month   = now.getMonth()+1; 
        var day     = now.getDate();
        var hour    = now.getHours();
        var minute  = now.getMinutes();
        var second  = now.getSeconds(); 
        if(month.toString().length == 1) {
             month = '0'+month;
        }
        if(day.toString().length == 1) {
             day = '0'+day;
        }   
        if(hour.toString().length == 1) {
             hour = '0'+hour;
        }
        if(minute.toString().length == 1) {
             minute = '0'+minute;
        }
        if(second.toString().length == 1) {
             second = '0'+second;
        }   
        var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second;   
         return dateTime;
    }
    
    链接地址: http://www.djcxy.com/p/77532.html

    上一篇: 在JSF页面上显示当前日期

    下一篇: 在JavaScript中获取当前日期和时间