Nodejs Date Format formatTime(pattern)

Here you can find the source of formatTime(pattern)

Method Source Code

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;
};

Related

  1. 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;
    };
    ...
    
  2. formatMMDD()
    Date.prototype.formatMMDD = function () {
       return this.getMonth() + 1 + '/' + this.getDate();
    };
    
  3. formatMMDDYYYY()
    Date.prototype.formatMMDDYYYY = function () {
       return this.getMonth() + 1 + '/' + this.getDate() + '/' + this.getFullYear();
    };
    
  4. formatStr()
    Date.prototype.formatStr=function(){
        function convert2fixed(str){
            var _num=Number(str);
            if(!isNaN(str)){
                if(_num<10){
                    return '0'+_num;
                }else
                return _num;
    ...
    
  5. 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(':');
    
  6. 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('');
    };
    
  7. 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(),
    ...
    
  8. formattedDate()
    Date.prototype.formattedDate = function(){
      var dd   = (this.getDate() < 10 ? "0" : "") + this.getDate();
      var mm   = (this.getMonth() + 1 < 10 ? "0" : "") + (this.getMonth() + 1);
      var yyyy = this.getFullYear();
      return yyyy + '/' + mm + '/' + dd;
    
  9. formattedDate(delemeter)
    Date.prototype.formattedDate = function (delemeter) {
      var dm = delemeter;
      if (!dm) dm = '';
      var yyyy = this.getFullYear().toString();
      var mm = (this.getMonth() + 1).toString();
      var dd = this.getDate().toString();
      if (mm < 10) mm = "0" + mm;
      if (dd < 10) dd = "0" + dd;
      return "" + yyyy + dm + mm + dm + dd;
    ...