JS 日付オブジェクトから YYYYMMDD 形式の文字列を取得しますか? 質問する

JS 日付オブジェクトから YYYYMMDD 形式の文字列を取得しますか? 質問する

date objectJS を使用して を 形式の文字列に変換しようとしています。 、、をYYYYMMDD連結するよりも簡単な方法はありますか?Date.getYear()Date.getMonth()Date.getDay()

ベストアンサー1

私がよく使用するコードを変更しました:

Date.prototype.yyyymmdd = function() {
  var mm = this.getMonth() + 1; // getMonth() is zero-based
  var dd = this.getDate();

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

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

おすすめ記事