標準オブジェクトの「Date」を利用した日付操作の方法について確認します。「西暦の取得」「月の取得」「日時の差分の取得方法」など取り上げます。
目次
Dateオブジェクト生成
コンストラクタ引数を指定しない場合、現在日時のDateオブジェクトが生成されます。
now = new Date()
// Mon Sep 03 2018 17:20:30 GMT+0900 (Japan Standard Time)
day = new Date(2018, 8, 10)
// Mon Sep 10 2018 00:00:00 GMT+0900 (Japan Standard Time)
day = new Date('2018-01-17T05:25:10')
// Wed Jan 17 2018 05:25:10 GMT+0900 (Japan Standard Time)月の扱いに注意
new Date(2018, 8, 10) と指定したとき、Sep(9月) となっています。
月は、1月 = 0 からスタートします。
get系メソッド
西暦を取得|getFullYear
day = new Date(2018, 8, 10, 03, 30, 20)
day.getFullYear() // 2018月を取得|getMonth
day = new Date(2018, 8, 10, 03, 30, 20)
day.getMonth() // 88 = 9月 であることに注意します。
日付を取得|getDate
day = new Date(2018, 8, 10, 03, 30, 20)
day.getDate() // 10時間を取得|getHours
day = new Date(2018, 8, 10, 03, 30, 20)
day.getHours() // 3分を取得|getMinutes
day = new Date(2018, 8, 10, 03, 30, 20)
day.getMinutes() // 30秒を取得|getSeconds
day = new Date(2018, 8, 10, 03, 30, 20)
day.getSeconds() // 20ミリ秒を取得|getMilliseconds
day = new Date(2018, 8, 10, 03, 30, 20)
day.getMilliseconds() // 0曜日番号を取得|getDay
day = new Date(2018, 8, 10, 03, 30, 20)
day.getDay() // 1| 番号 | 曜日 |
|---|---|
| 0 | 日曜 |
| 1 | 月曜 |
| 2 | 火曜 |
| 3 | 水曜 |
| 4 | 木曜 |
| 5 | 金曜 |
| 6 | 土曜 |
UNIXタイムスタンプを取得|getTime
ミリ秒単位 でUNIXタイムスタンプを取得します。
day = new Date(2018, 8, 10, 03, 30, 20)
day.getTime() // 1536517820000UNIXタイムスタンプ
1970年1月1日 00:00:00 UTC からの経過時間です。
set系メソッド
西暦を設定|setFullYear
day = new Date(2018, 8, 10, 03, 30, 20) // Mon Sep 10 2018 03:30:20 GMT+0900 (Japan Standard Time)
day.setFullYear(2015)
day // Thu Sep 10 2015 03:30:20 GMT+0900 (Japan Standard Time)月を設定|setMonth
day = new Date(2018, 8, 10, 03, 30, 20) // Mon Sep 10 2018 03:30:20 GMT+0900 (Japan Standard Time)
day.setMonth(0)
day // Wed Jan 10 2018 03:30:20 GMT+0900 (Japan Standard Time)日付を設定|setDate
day = new Date(2018, 8, 10, 03, 30, 20) // Mon Sep 10 2018 03:30:20 GMT+0900 (Japan Standard Time)
day.setDate(10)
day // Mon Sep 10 2018 03:30:20 GMT+0900 (Japan Standard Time)
day.setDate(40)
day // Wed Oct 10 2018 03:30:20 GMT+0900 (Japan Standard Time)時間を設定|setHours
day = new Date(2018, 8, 10, 03, 30, 20) // Mon Sep 10 2018 03:30:20 GMT+0900 (Japan Standard Time)
day.setHours(22)
day // Mon Sep 10 2018 22:30:20 GMT+0900 (Japan Standard Time)
day.setHours(30)
day // Tue Sep 11 2018 06:30:20 GMT+0900 (Japan Standard Time)分を設定|setMinutes
day = new Date(2018, 8, 10, 03, 30, 20) // Mon Sep 10 2018 03:30:20 GMT+0900 (Japan Standard Time)
day.setMinutes(11)
day // Mon Sep 10 2018 03:11:20 GMT+0900 (Japan Standard Time)
day.setMinutes(-20)
day // Mon Sep 10 2018 02:40:20 GMT+0900 (Japan Standard Time)秒を設定|setSeconds
day = new Date(2018, 8, 10, 03, 30, 20) // Mon Sep 10 2018 03:30:20 GMT+0900 (Japan Standard Time)
day.setSeconds(10)
day // Mon Sep 10 2018 03:30:10 GMT+0900 (Japan Standard Time)
day.setSeconds(61)
day // Mon Sep 10 2018 03:31:01 GMT+0900 (Japan Standard Time)ミリ秒を設定|setMilliseconds
day = new Date(2018, 8, 10, 03, 30, 20) // Mon Sep 10 2018 03:30:20 GMT+0900 (Japan Standard Time)
day.getMilliseconds() // 0
day.setMilliseconds(300)
day // Mon Sep 10 2018 03:30:20 GMT+0900 (Japan Standard Time)
day.getMilliseconds() // 300
day.setMilliseconds(2500)
day // Mon Sep 10 2018 03:30:22 GMT+0900 (Japan Standard Time)
day.getMilliseconds() // 500to系メソッド
言語環境に合わせて出力
( toLocaleString, toLocaleDateString )
day = new Date(2018, 8, 10, 03, 30, 20)
// 英語
day.toLocaleDateString('en-US') // "9/10/2018"
day.toLocaleString('en-US') // "9/10/2018, 3:30:20 AM"
// 日本語
day.toLocaleDateString('ja-JP') // "2018/9/10"
day.toLocaleString('ja-JP') // "2018/9/10 3:30:20"
// 第二引数のオプションを指定
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }
day.toLocaleDateString('ja-JP', options) // "2018年9月10日月曜日"日時の加算・減算
6ヶ月後
setMonthメソッド と getMonthメソッド を利用して、6ヶ月後 を求めてみます。
day = new Date(2018, 8, 10, 03, 30, 20) // Mon Sep 10 2018 03:30:20 GMT+0900 (Japan Standard Time)
day.setMonth(day.getMonth() + 6)
day // Sun Mar 10 2019 03:30:20 GMT+0900 (Japan Standard Time)7日前
setDateメソッド と getDateメソッド を利用して、7日前 を求めてみます。
day = new Date(2018, 8, 10, 03, 30, 20) // Mon Sep 10 2018 03:30:20 GMT+0900 (Japan Standard Time)
day.setDate(day.getDate() - 7)
day // Mon Sep 03 2018 03:30:20 GMT+0900 (Japan Standard Time)日時の差分
getTimeメソッド で UNIXタイムスタンプ を取得して差分を求めます。
day1 = new Date(2018, 8, 10, 03, 30, 20) // Mon Sep 10 2018 03:30:20 GMT+0900 (Japan Standard Time)
day2 = new Date(2018, 8, 21, 10, 05, 50) // Fri Sep 21 2018 10:05:50 GMT+0900 (Japan Standard Time)
diff = day2.getTime() - day1.getTime() // 974130000
// 日数の差分
diff / (24 * 60 * 60 * 1000) // 11.274652777777778
// 時間の差分
diff / (60 * 60 * 1000) // 270.59166666666664