LoginSignup
1
1

More than 3 years have passed since last update.

【Javascript】ワンライナーで現在時刻をフォーマットした日付で表示する

Last updated at Posted at 2020-06-29

getDay()、getHours()とかやってから繋げるのめんどくさい:confounded:

コード:writing_hand:

ワンライナー
new Date().toLocaleString(undefined, { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit" });
↑改行するとこう
new Date().toLocaleString(undefined, {
  month: "short",
  day: "numeric",
  hour: "2-digit",
  minute: "2-digit"
});
結果
8月10日 19:19

別のやり方

new Intl.DateTimeFormat(undefined, { [ここにオプション] }).format(new Date());

使い方

dateObj.toLocaleString([locales[, options]])

詳細:Date.prototype.toLocaleString()

  • locales "ja"とかが入ります。undefinedなら、OSのデフォルトです
  • options フォーマットを指定できます(自由度は低い)。詳しくはMDNで

※ localesが日本だと"2-digit"で表示されないことがあります:innocent:

まとめ

moment.jsのが楽です:hugging:

1
1
0

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