【徹底解説】date-fns Year Helpers:subYears関数完全ガイド
date-fns の Year Helpers と subYears 関数の使い方
subYears
関数は、指定された日付から指定された年数を引いた新しい日付を返す関数です。例えば、今日から3年を引いた日付を知りたい場合は、次のように記述します。
import { subYears } from 'date-fns';
const today = new Date();
const threeYearsAgo = subYears(today, 3);
console.log(threeYearsAgo); // 2021-03-17T00:00:00.000Z
このコードは、today
という変数に今日の日付を格納し、subYears
関数を使って3年前にあたる日付を計算し、threeYearsAgo
という変数に格納しています。
subYears
関数は以下の2つの引数を受け取ります。
- date: 操作対象となる日付オブジェクト
- years: 引く年数
subYears
関数は、様々な方法で使用することができます。
- 特定の日付から年数を引く
const date = new Date(2024, 3, 17);
const oneYearAgo = subYears(date, 1);
console.log(oneYearAgo); // 2023-03-17T00:00:00.000Z
- 現在の日付から年数を引く
const today = new Date();
const twoYearsAgo = subYears(today, 2);
console.log(twoYearsAgo); // 2022-03-17T00:00:00.000Z
- 変数に格納された日付から年数を引く
const dateStr = '2025-03-17';
const date = new Date(dateStr);
const threeYearsAgo = subYears(date, 3);
console.log(threeYearsAgo); // 2022-03-17T00:00:00.000Z
subYears
関数は、日付オブジェクトを引数として受け取ります。- 引く年数は整数値で指定する必要があります。
subYears
関数は、元の Date オブジェクトを変更しません。新しい Date オブジェクトを返します。
subYears
関数は、JavaScript で日付を扱う際に非常に便利な関数です。この関数をマスターすることで、様々な日付操作を簡単に実行することができます。
subYears 関数のサンプルコード
特定の日付から年数を引く
const date = new Date(2024, 3, 17);
// 1年前にあたる日付
const oneYearAgo = subYears(date, 1);
console.log(oneYearAgo); // 2023-03-17T00:00:00.000Z
// 5年前にあたる日付
const fiveYearsAgo = subYears(date, 5);
console.log(fiveYearsAgo); // 2019-03-17T00:00:00.000Z
現在の日付から年数を引く
const today = new Date();
// 2年前にあたる日付
const twoYearsAgo = subYears(today, 2);
console.log(twoYearsAgo); // 2022-03-17T00:00:00.000Z
// 10年前にあたる日付
const tenYearsAgo = subYears(today, 10);
console.log(tenYearsAgo); // 2014-03-17T00:00:00.000Z
変数に格納された日付から年数を引く
const dateStr = '2025-03-17';
const date = new Date(dateStr);
// 3年前にあたる日付
const threeYearsAgo = subYears(date, 3);
console.log(threeYearsAgo); // 2022-03-17T00:00:00.000Z
// 4年前にあたる日付
const fourYearsAgo = subYears(date, 4);
console.log(fourYearsAgo); // 2021-03-17T00:00:00.000Z
年数を動的に指定する
const yearDiff = 5;
const today = new Date();
const pastDate = subYears(today, yearDiff);
console.log(pastDate); // 2019-03-17T00:00:00.000Z
月と日も指定する
const date = new Date(2024, 3, 17);
// 1年前にあたる日付、ただし月は1月、日は1日とする
const oneYearAgo = subYears(date, 1, { month: 0, day: 1 });
console.log(oneYearAgo); // 2023-01-01T00:00:00.000Z
// 5年前にあたる日付、ただし月は12月、日は31日とする
const fiveYearsAgo = subYears(date, 5, { month: 11, day: 31 });
console.log(fiveYearsAgo); // 2019-12-31T00:00:00.000Z
subYears 関数以外の方法
Date オブジェクトのメソッドを使う
Date オブジェクトには、getFullYear()
や setFullYear()
などのメソッドが用意されています。これらのメソッドを使って、年数を直接操作することができます。
const date = new Date(2024, 3, 17);
// 年数を取得
const year = date.getFullYear();
// 年数を1年減らす
date.setFullYear(year - 1);
console.log(date); // 2023-03-17T00:00:00.000Z
ミリ秒単位で計算する
Date オブジェクトは、ミリ秒単位で時間を表します。そのため、ミリ秒換算で年数を計算し、その値を引くことで、日付から年数を引くことができます。
const date = new Date(2024, 3, 17);
// 1年分のミリ秒数
const oneYearInMilliseconds = 31556952000;
// 1年前にあたる日付
const oneYearAgo = new Date(date.getTime() - oneYearInMilliseconds);
console.log(oneYearAgo); // 2023-03-17T00:00:00.000Z
ライブラリを使う
date-fns
以外にも、JavaScript で日付を扱うためのライブラリはたくさんあります。これらのライブラリの中には、subYears
関数と同様の機能を提供しているものもあります。
これらのライブラリを使う場合は、それぞれの使い方をドキュメントで確認する必要があります。
subYears
関数以外にも、JavaScript で日付から年数を引く方法はいくつかあります。それぞれの方法にはメリットとデメリットがありますので、用途に合わせて使い分けてください。
date-fns vs Moment.js:JavaScriptで日付を扱うためのライブラリ徹底比較
General カテゴリ のトークンは、日付の一般的な部分を表します。以下に、よく使用されるトークンとその意味をまとめました。これらのトークンを組み合わせて、さまざまな書式設定文字列を作成できます。例えば、以下の書式設定文字列は、西暦年(4桁)
ISO 週年の最後の瞬間を取得:date-fns "endOfISOWeekYear" ヘルパーとその他の方法
endOfISOWeekYear ヘルパーは、特定の日付の「ISO 週年」の最後の瞬間を返す関数です。ISO 週年は、1月1日から始まる週で構成される年です。使い方説明date-fns ライブラリを import します。endOfISOWeekYear ヘルパーに Date オブジェクトを渡します。
【フロントエンドエンジニア必見】date-fns hoursToMinutesで時間を分に変換する方法
date-fnsライブラリのConversion Helpersは、日付と時刻の操作を簡潔に行うための関数群を提供します。その中のhoursToMinutes関数は、時間数を分に変換する関数です。使い方引数hours: 変換したい時間数を表す数値
differenceInCalendarISOWeekYears 関数って?2つの日付間のISO週番号年数の差をカンタンに計算する方法
differenceInCalendarISOWeekYears 関数は、2つの日付間のISO週番号年数の差を計算します。ISO週番号年数は、ISO 8601 規格で定義された年単位の期間で、1月4日から始まり、12月31日まで続きます。使い方
closestIndexToでできること:日付配列から最も近い日付を簡単に見つけよう
"date-fns" の "Common Helpers" に含まれる closestIndexTo 関数は、日付の配列の中で、指定された日付に最も近い日付のインデックスを取得します。これは、カレンダーアプリやイベント管理システムなど、日付を扱う様々な場面で役立つ機能です。
【プログラミング】date-fnsの"startOfWeekYear"関数で週番号年の最初の週を計算する方法
"date-fns" ライブラリに含まれる "Week-Numbering Year Helpers" カテゴリの "startOfWeekYear" 関数は、指定された日付を含む週番号年の最初の週の開始時刻を返します。つまり、その年の最初の週がいつ始まるかを計算します。