How to get current date in jquery?

前端 未结 30 1902
春和景丽
春和景丽 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
    

提交回复
热议问题