준호씨의 블로그

javascript - date yyyymmdd, hhmmss, yyyymmddhhmmss, yyyymmddhhmm 본문

개발이야기

javascript - date yyyymmdd, hhmmss, yyyymmddhhmmss, yyyymmddhhmm

준호씨 2018. 7. 11. 22:00
반응형

yyyymmdd

Date.prototype.yyyymmdd = function() {
  var mm = this.getMonth() + 1;
  var dd = this.getDate();

  return [this.getFullYear(),
          (mm>9 ? '' : '0') + mm,
          (dd>9 ? '' : '0') + dd
         ].join('');
};

var date = new Date();
date.yyyymmdd();

output

"20180703"

hhmmss

Date.prototype.hhmmss = function() {
  var hh = this.getHours();
  var mm = this.getMinutes();
  var ss = this.getSeconds();

  return [(hh>9 ? '' : '0') + hh,
          (mm>9 ? '' : '0') + mm,
          (ss>9 ? '' : '0') + ss,
         ].join('');
};

var date = new Date();
date.hhmmss();

output

"165731"

yyyymmddhhmmss

Date.prototype.yyyymmddhhmmss = function() {
  return this.yyyymmdd() + this.hhmmss();
};

var date = new Date();
date.yyyymmddhhmmss();

output

"20180703165742"

yyyymmddhhmm

date.yyyymmddhhmmss().substring(0,12)

output

"201807031657"

참고

반응형
Comments