js converts the number of seconds and milliseconds into a time string

/**
   * 将秒数转换成:某年某月某日 00:00:00 格式的时间串
   */
  formatDate:function(date){
    //date是秒数
    var d = new Date(date*1000),
	month = '' + (d.getMonth() + 1),
	day = '' + d.getDate(),
    year = d.getFullYear(),
    hour = d.getHours(),
    minute = d.getMinutes(),
    seconds = d.getSeconds();
	// if (month.length < 2) month = '0' + month;
    // if (day.length < 2) day = '0' + day;
    // if (hour.length < 2) hour = '0' + hour;
    // if (minute.length < 2) minute = '0' + minute;
    // if (seconds.length < 2) seconds = '0' + seconds;
    month = parseInt(month) < 10?('0'+month):month;
    day = parseInt(day) < 10?('0'+day):day;
    hour = parseInt(hour) < 10?('0'+hour):hour;
    minute = parseInt(minute) < 10?('0'+minute):minute;
    seconds = parseInt(seconds) < 10?('0'+seconds):seconds;
    return year+"年"+month+"月"+day+"日"+" "+hour+":"+minute+":"+seconds;
  },

Note that you cannot use length to judge the length to add 0, and it will not be added, as shown in the figure:

Guess you like

Origin blog.csdn.net/qq_41588568/article/details/108239650