0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavascriptのDate.prototype.toString()系まとめ

Posted at

new Date()からタイムスタンプを作ってファイル名にしたい時、どのフォーマットにするか毎回調べてるのでまとめる

メソッド                出力される文字列                      備考
new Date('2000-01-01 00:00:00').toString() "Sat Jan 01 2000 00:00:00 GMT+0900 (日本標準時)"
new Date('2000-01-01 00:00:00').toISOString() "1999-12-31T15:00:00.000Z"
new Date('2000-01-01 00:00:00').toLocaleString() "2000/1/1 0:00:00"
new Date('2000-01-01 00:00:00').toGMTString() "Fri, 31 Dec 1999 15:00:00 GMT" 非推奨 .toUTCString()を使う
new Date('2000-01-01 00:00:00').toUTCString() "Fri, 31 Dec 1999 15:00:00 GMT"
new Date('2000-01-01 00:00:00').toDateString() "Sat Jan 01 2000"
new Date('2000-01-01 00:00:00').toLocaleDateString() "2000/1/1"
new Date('2000-01-01 00:00:00').toTimeString() "00:00:00 GMT+0900 (日本標準時)"
new Date('2000-01-01 00:00:00').toLocaleTimeString() "0:00:00"

おまけ
moment.jsでお手軽にタイムスタンプを作りたいとき(よくやる)

// const moment = require('moment');
import moment from 'moment';
const now = moment().format('YYYY-MM-DD HH:mm:ss.SSS');
now; // 2000-01-01 00:00:00.000

hourを00~23で表したいときは、大文字でHH
小文字でhhだと1~12になってしまう

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?