How to get current date in jquery?

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

    Using pure Javascript your can prototype your own YYYYMMDD format;

    Date.prototype.yyyymmdd = function() {
      var yyyy = this.getFullYear().toString();
      var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
      var dd  = this.getDate().toString();
      return yyyy + "/" + (mm[1]?mm:"0"+mm[0]) + "/" + (dd[1]?dd:"0"+dd[0]); // padding
    };
    
    var date = new Date();
    console.log( date.yyyymmdd() ); // Assuming you have an open console
    

提交回复
热议问题