LoginSignup
1
1

More than 3 years have passed since last update.

Javascript 現在の日付を「YYYY年MM月DD日」の形で取得

Last updated at Posted at 2020-05-14

日付を文字列として取得

Javascriptで日付を取得しても、画面に表示したい時に困ることが多い。
と言うわけで、こんな形で取得すれば困ることはないかと。

// 現在の月を「2020年5月10日」みたいな形で取得
  getMonthString() {
    let dt = new Date();
    let y = dt.getFullYear();
    let m = ('00' + (dt.getMonth() +1 )).slice(-2);
    let d = ("00" + dt.getDate()).slice(-2);
    const result = y + '' + m + '' + d + '';
    console.log(result);
    return result;
  }

こちらが参考になりました:blush:

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1