今回は Node.js で Date 型を扱いやすくする date-utils の使い方をご紹介します。
Contents [hide]
環境
- Node.js v8.11.3
- npm v5.6.0
使用するライブラリはこちら
→ JerrySievert/date-utils
date-utils の使い方
インストール
npm を使ってインストールを行っていきます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm install date-utils |
app.js 内での使い方
import か require を行うことで、Date 型が拡張されるようになります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('date-utils'); |
日付フォーマット変更(.toFormat)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let nowFormatted = new Date().toFormat("YYYY年MM月DD日 HH24時MI分SS秒"); | |
// return 2018年08月30日 15時51分14秒 | |
console.log(nowFormatted); |
本日の日付を取得(.today)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let today = Date.today(); | |
console.log(today); |
昨日の日付を取得(.yesterday)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let yesterday = Date.yesterday(); | |
console.log(yesterday); |
明日の日付を取得(.tomorrow)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let tomorrow = Date.tomorrow(); | |
console.log(tomorrow); |
日付を比較(.compare)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let today = Date.today(); | |
let tomorrow = Date.tomorrow(); | |
let yesterday = Date.yesterday(); | |
// return 0 | |
let compareTodayToday = Date.compare(today, today); | |
console.log(compareTodayToday); | |
// return -1 | |
let compareTodayTomorrow = Date.compare(today, tomorrow); | |
console.log(compareTodayTomorrow); | |
// return 1 | |
let compareTomorrowToday = Date.compare(tomorrow, today); | |
console.log(compareTomorrowToday); |
日付を比較(.equals)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let today = Date.today(); | |
let tomorrow = Date.tomorrow(); | |
let yesterday = Date.yesterday(); | |
// return true | |
console.log(Date.equals(today, today)); | |
// return false | |
console.log(Date.equals(today, tomorrow)); |
指定年月の日数を取得(.getDaysInMonth)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// return 31 | |
console.log(Date.getDaysInMonth(2018, 8 + 1)); |
日付の形式チェック(.validateDay)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// return true | |
console.log(Date.validateDay(31, 2018, 8 + 1)); | |
// return false | |
console.log(Date.validateDay(32, 2018, 8 + 1)); |
今日よりも後かチェック(.isAfter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let yesterday = Date.yesterday(); | |
let tomorrow = Date.tomorrow(); | |
// return true | |
console.log(tomorrow.isAfter()); | |
// return false | |
console.log(yesterday.isAfter()); |
今日よりも前かチェック(.isBefore)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let yesterday = Date.yesterday(); | |
let tomorrow = Date.tomorrow(); | |
// return true | |
console.log(yesterday.isBefore()); | |
// return false | |
console.log(tomorrow.isBefore()); |
日付加算(.add)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let today = Date.today(); | |
// milliseconds OR seconds OR minutes OR hours OR days OR weeks OR months OR years | |
console.log(today..add({"months": 2, "days": 1})); |
日付減算(.remove)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let today = Date.today(); | |
// milliseconds OR seconds OR minutes OR hours OR days OR weeks OR months OR years | |
console.log(today.remove({"months": 2, "days": 1})); |
色々と調べてやってみましたが、残念がら結局 moment.js が一番使いやすいような気がしました…笑
次は moment.js の使い方についてご紹介しようと思います。
→ moment.js の使い方についてもまとめてみました