LoginSignup
94
63

More than 5 years have passed since last update.

逆引きLuxon集 〜moment.jsから乗り換えるために〜

Last updated at Posted at 2018-07-12

こちらはあら便利カレンダー2018の記事です。

jsfiddleでLuxon playground作りました!
http://jsfiddle.net/nobu222/p49m62k7/

Luxonは時間を扱いやすくするためのJavaScriptライブラリ。
すでに有名なmoment.jsと同じGithubリポジトリで開発されている、ネクストmoment.js的な位置づけのやつです。

特徴

DateTime, Duration, and Interval types.
Immutable, chainable, unambiguous API.
Parsing and formatting for common and custom formats.
Native time zone and Intl support (no locale or tz files).

公式より抜粋

  • 時間に関する日時、量、期間を扱う3つのクラスが内包されていて
  • イミュータブルですよ!
  • タイムゾーンとかも最初からサポートされてますよ!

といった感じです。
momentでしんどかった部分(イミュータブル & タイムゾーン)が解消された、用途に応じた各クラスがえらい強力になった、などなど
使ってみてとてもよかったのですが、
日本語の記事はまだあんまなさげ、QiitaにもLuxonタグなかったのでまとめ記事かきました。

以下、逆引き形式で使い方を紹介します。
内容はほぼ公式ドキュメントを和訳して逆引きにしただけです。

逆引きLuxon

現在時刻で取得

DateTime.local();

日時指定で取得

DateTime.local(2017, 5, 15, 8, 30);

DateTimeを比較

const oneDay = DateTime.local();
const someDay = DateTime.local(2018, 8, 14, 8, 30);
if (oneDay > someDay) console.log('oneDay after someDay');
// log...oneDay after someDay

タイムゾーンをUTCにする

const dateTime = DateTime.local();
const utcDateTime = dateTime.toUTC();

タイムゾーン指定

DateTime.fromObject({zone: 'America/Los_Angeles'}) // ロサンゼルス時間の今
DateTime.local().setZone('America/Los_Angeles') // こっちでも同じ

フォーマットを指定して文字列出力

const dateTime = DateTime.local();
dateTime.toFormat('yyyy/MM/dd');

日時を足す・引く・セットする

var dt = DateTime.local();
dt.plus({hours: 3, minutes: 2});
dt.minus({days: 7});
dt.set({hour: 3});

日時の始まり、終わりを取得

dt.startOf('month');
dt.endOf('hour');

指定量の時間を取得

Duration.fromObject({hours: 2, minutes: 7}); // 2時間7分

違う単位の時間を足す、引く

const year = Duration.fromObject({years: 1});
const weeks = Duration.fromOject({weeks: 100});
const diff = year.minus(weeks);// 日で考えると 365 - 700 になる

指定期間を扱う

now = DateTime.local();
later = DateTime.local(2020, 10, 12);
i = Interval.fromDateTimes(now, later);
i.length('years'); // 今から2020/10/12まで

指定の期間に含まれているかチェック(期間、日時)

const now = DateTime.local();
const start = DateTime.local(2020,7,24);
const olympic = Interval.fromDateTimes(start, start.plus(days: 16));
olympic.contain(now); // 今が2020/7/24-2020/8/9に含まれているか => false

指定の期間に含まれているかチェック(期間、期間)

const now = DateTime.local();
const tomorrow = now.plus({'day': 1});
const twoDays = Interval.fromDateTimes(now, tomorrow);
const start = DateTime.local(2020,7,24);
const olympic = Interval.fromDateTimes(start, start.plus(days: 16));
olympic.overlaps(twoDays); // 現在-明日までの期間が2020/7/24-2020/8/9との重複があるか => false

ある期間から複数の期間で重複しない期間を取得

const plans = [
  Interval.fromDateTimes(~~~~),
  Interval.fromDateTimes(~~~~),
  Interval.fromDateTimes(~~~~)
];
const now = DateTime.local();
const thisMonth = Interval.fromDateTimes(now.startOf('month'), now.endOf('month'));
const availables = thisMonth.difference(...plans); // 予定A,B,Cがあり今月予定のない期間をすべて取得する、みたいな

2つの期間の積集合を取得

const intervalOne = Interval.fromDateTimes(~~~~);
const intervalTwo = Interval.fromDateTimes(~~~~);
const intersection = intervalOne.intersection(intervalTwo); 

2つの期間の和集合を取得

const intervalOne = Interval.fromDateTimes(~~~~);
const intervalTwo = Interval.fromDateTimes(~~~~);
const union = intervalOne.union(intervalTwo);

もっと時間の計算に便利な機能がたくさんあるのですが、、

もしリクエストとかあればこの記事を更新するかもです!!
ひとまず力尽きたのでこのへんで。

94
63
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
94
63