Quantcast
Viewing all articles
Browse latest Browse all 8695

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

getDay()、getHours()とかやってから繋げるのめんどくさいImage may be NSFW.
Clik here to view.
:confounded:

コードImage may be NSFW.
Clik here to view.
:writing_hand:

ワンライナー
newDate().toLocaleString(undefined,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"});
↑改行するとこう
newDate().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"で表示されないことがありますImage may be NSFW.
Clik here to view.
:innocent:

まとめ

moment.jsのが楽ですImage may be NSFW.
Clik here to view.
:hugging:


Viewing all articles
Browse latest Browse all 8695

Trending Articles