0
0

More than 3 years have passed since last update.

[日付の表示]yyyy/mm/ddのフォーマットにしたい

Posted at

備忘録。

WebアプリでTo-doリストを作成中。
アイテムを作成した日付を付与し、アイテムと一緒に表示させたい。

その際、日付のフォーマットをyyyy/mm/ddにする 。←本記事で書くこと
(※9以下の数字には左に0を置きたい)

日時の前に「00」、後に「slice(-2)」を置くことで実現!

※Nuxtで書いているので;省略などJsの文法無視しています
※ 記事作成日: 2021年06月21日(日本)

const d = new Date()
const fmt = d.getFullYear()

// document.write(d) 
// => Mon Jun 21 2021 11:32:57 GMT+0900 (日本標準時)

// 年の表示
document.write(fmt) // => 2021
document.write('/')

// 月の表示
document.write(d.getMonth() + 1) // => 6
document.write('/')

// 月の表示(9以下の数字で左に0を置く)
document.write(('00' + (d.getMonth() + 1)).slice(-2)) // => 06
document.write('/')

// 日にちの表示
document.write(('00' + d.getDate()).slice(-2)) //=>21
0
0
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
0
0