How to get current date in jquery?

前端 未结 30 1747
春和景丽
春和景丽 2020-11-27 09:51

I want to know how to use the Date() function in jQuery to get the current date in a yyyy/mm/dd format.

相关标签:
30条回答
  • 2020-11-27 10:03

    I just wanted to share a timestamp prototype I made using Pierre's idea. Not enough points to comment :(

    // US common date timestamp
    Date.prototype.timestamp = function() {
      var yyyy = this.getFullYear().toString();
      var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
      var dd  = this.getDate().toString();
      var h = this.getHours().toString();
      var m = this.getMinutes().toString();
      var s = this.getSeconds().toString();
    
      return (mm[1]?mm:"0"+mm[0]) + "/" + (dd[1]?dd:"0"+dd[0]) + "/" + yyyy + " - " + ((h > 12) ? h-12 : h) + ":" + m + ":" + s;
    };
    
    d = new Date();
    
    var timestamp = d.timestamp();
    // 10/12/2013 - 2:04:19
    
    0 讨论(0)
  • 2020-11-27 10:05

    If you have jQuery UI (needed for the datepicker), this would do the trick:

    $.datepicker.formatDate('yy/mm/dd', new Date());
    
    0 讨论(0)
  • 2020-11-27 10:05
    //convert month to 2 digits<p>
    var twoDigitMonth = ((fullDate.getMonth().length+1) === 1)? (fullDate.getMonth()+1) : '0' + (fullDate.getMonth()+1);
    
    var currentDate =  fullDate.getFullYear()+ "/" + twoDigitMonth + "/" + fullDate.getDate();
    console.log(currentDate);<br>
    //2011/05/19
    
    0 讨论(0)
  • 2020-11-27 10:09

    The jQuery plugin page is down. So manually:

    function strpad00(s)
    {
        s = s + '';
        if (s.length === 1) s = '0'+s;
        return s;
    }
    
    var now = new Date();
    var currentDate = now.getFullYear()+ "/" + strpad00(now.getMonth()+1) + "/" + strpad00(now.getDate());
    console.log(currentDate );
    
    0 讨论(0)
  • 2020-11-27 10:09
    console.log($.datepicker.formatDate('yy/mm/dd', new Date()));
    
    0 讨论(0)
  • 2020-11-27 10:10

    Try this....

    var d = new Date();
    alert(d.getFullYear()+'/'+(d.getMonth()+1)+'/'+d.getDate());
    

    getMonth() return month 0 to 11 so we would like to add 1 for accurate month

    Reference by : http://www.w3schools.com/jsref/jsref_obj_date.asp

    0 讨论(0)
提交回复
热议问题