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?

日付フォーマット用ライブラリの選定

Posted at

日付を扱うために

個人で開発しているアプリで日付を扱う必要があり,
ライブラリを導入することにしました。

候補1 moment.js

mutable(変更する際は元の変数を書き換える)

メリット

  • 豊富な機能:日付の加算や減算、フォーマット、相対日時、ローカライズなど、多くの機能

デメリット

  • 2020年以降、使用は非推奨
  • ローカルタイムを表示するためには、プラグインを導入する必要がある
スター数 最終更新日 ファイルサイズ 確認日
48.1k 2023-12-27 2kb 2025/3/10
import moment from 'moment';

let newDate: moment.Moment =
  newDate: moment().add(1, 'days'); // 一日加算

公式github
https://github.com/moment/moment

候補2 day.js

Immutable(変更する際は、新しい変数に格納。変更するごとに変数が増える。新しく変数を作成し、最新のものを読み取るメゾットが必要。)

メリット

  • 軽量
  • 全てのブラウザで対応
  • 必要な機能だけを追加できるプラグインシステム

デメリット

  • ローカルタイムを表示するためには、プラグインを導入する必要がある
スター数 最終更新日 ファイルサイズ 確認日
47.5k 2024-08-20 2mb 2025/3/8
import dayjs from 'dayjs';

let currentDate: DateTime = dayjs()
let newDate: DateTime =
  newDate: dayjs().add(1, 'day'); // 一日加算

公式github
https://github.com/iamkun/dayjs

候補3 date.fns

Immutable

メリット

  • 多くの日付操作機能がある
  • 個別の関数で処理を行い、必要な処理だけを取り込めるため、非常に軽量
  • 型定義が充実

デメリット

  • ローカルタイムを表示するためには、プラグインを導入する必要がある
スター数 最終更新日 ファイルサイズ 確認日
35.4k 2024-09-17 22.6 MB 2025/3/8
import { addDays } from 'date-fns';

let currentDate: Date = new Date();
let newDate: Date = addDays(currentDate, 1);

公式github
https://github.com/date-fns/date-fns

候補4 Luxon.js

monent.jsの後続として開発。
Immutable

メリット

  • ローカルタイムがプラグインをいれなくとも、利用できる
  • 複数のローカルタイムに対応

デメリット

  • ファイルサイズが大きい。(処理が重くなる)
  • ローカルタイムを使用しない場合は過剰
スター数 最終更新日 ファイルサイズ 確認日
15.7k 2024-08-04 4.48 MB 2025/3/8
import { DateTime } from 'luxon';

let currentDate: DateTime = DateTime.now(); // 現在の日付
let newDate: DateTime = 
  newDate: currentDate.plus({ days: 1 }); // 一日加算

公式github
https://github.com/moment/luxon

まとめ

順位 ライブラリ 理由
1 date.fns 型定義が充実し、デフォルトのdateに似ている
2 day.js 軽量であり、全てのブラウザに対応している
3 Luxon.js 処理が重いこと
4 moment.js 現在は仕様が推奨されていないこと

4つの候補から「date.fns」を使用することにします。

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?