Nodejs Date Format formatStr()

Here you can find the source of formatStr()

Method Source Code

Date.prototype.formatStr=function(){
    function convert2fixed(str){
        var _num=Number(str);
        if(!isNaN(str)){
            if(_num<10){
                return '0'+_num;
            }else// www. jav a  2s.  com
            {
            return _num;
            }
        }
        return 0;
    }
    var _year=this.getFullYear(),
        _month=this.getMonth()+1,
        _day=this.getDate(),
        _hours=convert2fixed(this.getHours()),
        _minutes=convert2fixed(this.getMinutes()),
        _seconds=convert2fixed(this.getSeconds());
    return _year+"-"+_month+"-"+_day+" "+_hours+":"+_minutes+":"+_seconds;
}

Related

  1. formatDate(format)
    Date.prototype.formatDate = function (format) {
        var date = this,
            day = date.getDate(),
            month = date.getMonth() + 1,
            year = date.getFullYear(),
            hours = date.getHours(),
            minutes = date.getMinutes(),
            seconds = date.getSeconds();
        if (!format) {
    ...
    
  2. formatDate(format)
    Date.prototype.formatDate = function(format) {
      var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
      var yyyy = this.getFullYear();
      var yy = yyyy.toString().substring(2);
      var m = this.getMonth() + 1;
      var mm = m < 10 ? "0" + m : m;
      var mmm = months[m - 1];
      var d = this.getDate();
      var dd = d < 10 ? "0" + d : d;
    ...
    
  3. formatDay(pattern, dayNames = [], monthnames = [])
    Date.prototype.formatDay = function (pattern, dayNames = [], monthnames = []) {
        pattern = pattern.replace(/dddd/g, dayNames[this.getDay()]);
        pattern = pattern.replace(/dd/g, this.getDate().toString().padLeft(2, 0));
        pattern = pattern.replace(/mmm/g, monthnames[this.getMonth()]);
        pattern = pattern.replace(/mm/g, (this.getMonth() + 1).toString().padLeft(2, 0));
        pattern = pattern.replace(/yyyy/g, this.getFullYear().toString());
        pattern = pattern.replace(/yy/g, this.getFullYear().toString().substr(2, 2));
        return pattern;
    };
    ...
    
  4. formatMMDD()
    Date.prototype.formatMMDD = function () {
       return this.getMonth() + 1 + '/' + this.getDate();
    };
    
  5. formatMMDDYYYY()
    Date.prototype.formatMMDDYYYY = function () {
       return this.getMonth() + 1 + '/' + this.getDate() + '/' + this.getFullYear();
    };
    
  6. formatTime(date)
    function formatTime(date) {
      if(date instanceof Date === false) {
        return console.log('err');
      var hour = pad(date.getHours());
      var minute = pad(date.getMinutes());
      return [hour, minute].join(':');
    
  7. formatTime(pattern)
    Date.prototype.formatTime = function (pattern) {
        pattern = pattern.replace(/hh/g, this.getHours().toString().padLeft(2, 0));
        pattern = pattern.replace(/mm/g, this.getMinutes().toString().padLeft(2, 0));
        return pattern;
    };
    
  8. formateDate()
    Date.prototype.formateDate = function() {
      var mm = this.getMonth() + 1;
      var dd = this.getDate();
      return [this.getFullYear(), !mm[1] && '0', mm, !dd[1] && '0', dd].join('');
    };
    
  9. formatf(format)
    Date.prototype.format = function f(format) {
        const o = {
            'M+': this.getMonth() + 1,
            'd+': this.getDate(),
            'h+': this.getHours(),
            'm+': this.getMinutes(),
            's+': this.getSeconds(),
            'q+': Math.floor((this.getMonth() + 3) / 3),
            S: this.getMilliseconds(),
    ...