10yroの開発日記

福岡にある株式会社10yro(トイロ)のエンジニアが書いています

【JavaScript/TypeScript】日付操作のいろいろ

JavaScriptで日付型(Date)を扱う際、ある日付の月初、月末を取る、決算期として期の開始を取得 等々

日付を加工して利用したいケースが多々あるかと思います。

今回、日付操作に関する方法を残しておきたいと思います。


目次:



1. 年を取得 (number)

getYear(date: Date): number {
    return date.getFullYear();
}

例)2022/02/07 13:00:00 (Date) ⇒ 2022 (number)

2. 月を取得 (number)

getMonth(date: Date): number {
    return date.getMonth() + 1;
}

※ getMonth() は 0 start (1月=0, 2月=1 ...)のため +1 するのをお忘れなく
例)2022/02/07 13:00:00 (Date) ⇒ 2 (number)

3. 日を取得 (number)

getDay(date: Date): number {
    return date.getDate();
}

例)2022/02/07 13:00:00 (Date) ⇒ 7 (number)

4. 月初を取得 (Date)

getFirstDateTimeOfMonth(date: Date): Date {
    return new Date(date.getFullYear(), date.getMonth(), 1, 0, 0, 0, 0);
}

例)2022/02/07 13:00:00 (Date) ⇒ 2022/02/01 00:00:00.000 (Date)
時刻も最初 00:00:00.000 を指定しています。
時間はdateのものとしたい場合は末尾の引数 「0, 0, 0, 0」 を↓に置き換えればOK
date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()

5. 月末を取得 (Date)

getLastDateTimeOfMonth(date: Date): Date {
    return new Date(date.getFullYear(), date.getMonth() + 1, 0, 23, 59, 59, 999);
}

例)2022/02/07 13:00:00 (Date) ⇒ 2022/02/28 23:59:59:999 (Date)
時刻も最後 23:59:59.999 を指定しています。
ポイントは月を +1 して 日を 0 (-1) としているところです。

6. 決算年度を取得 (number)

getFiscicalYear(date: Date): number {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;     
    let tmpQuarter = Math.floor((month - 1) / 3) + 1;
    const quarter = tmpQuarter - 1 === 0 ? 4 : tmpQuarter - 1;
    return quarter == 4 ? year - 1 : year;
}

例)2022/02/07 13:00:00 (Date) ⇒ 2021 (number)
4半期(3月決算)を前提とした値(計算)となっています。
4半期のため1期3か月なので Math.floor((month - 1) / 3) しています。
※それに+1しているのは1-3月が0になるため1始まりにしています。(特に+1せず期を算出する際の -1を消してもOKです。)
そして、3月決算で4月始まりの為1-3月は4とし、その他を繰り下げます。
後は4期は前年度にあたるため 年は -1 になります。 これは「7. 決算期を取得」の内容を絡め記載してますが、決算年度を得るだけであれば以下のように -3 ヶ月したものの年を求めることでもできます。

getFiscicalYear(date: Date): number {
        let date = new Date();
        date.setMonth(xxx.getMonth() - 3);
        return date.getFullYear();
}

7. 決算期を取得 (number)

getQuarter(date: Date): number {
    const month = date.getMonth() + 1;
    let quarter = Math.floor((month - 1) / 3) + 1;
    return quarter - 1 === 0 ? 4 : quarter - 1;
}

例)2022/02/07 13:00:00 (Date) ⇒ 4 (number)
4半期(3月決算)を前提とした値(計算)となっています。

8. 期の最初の日付を取得 (Date)

getFirstDateTimeOfQuarter(year: number, quarter: number): Date {
    const month = (quarter - 1) * 3 + 1;
    const date = new Date(year, month - 1, 1);
    // fiscal year end is March (+3 : Shift one fiscal year)
    date.setMonth(date.getMonth() + 3);
    return Date(date.getFullYear(), date.getMonth(), 1, 0, 0, 0, 0);
}

例)year = 2021 / quarter = 4 ⇒ 2022/01/01 00:00:00 (Date)
4半期(3月決算)を前提とした値(計算)となっています。
※ここでは12月決算(1月始まり)で計算し、後に+3か月しています。
時刻も最初 00:00:00.000 を指定しています。

9. 期の最後の日付を取得 (Date)

getLastDateTimeOfQuarter(year: number, quarter: number): Date {
    const month = quarter * 3;
    const date = new Date(year, month - 1, 1);
    // fiscal year end is March (+3 : Shift one fiscal year)
    date.setMonth(date.getMonth() + 3);
    return new Date(date.getFullYear(), date.getMonth() + 1, 0, 23, 59, 59, 999);
}

例)year = 2021 / quarter = 4 ⇒ 2022/03/31 23:59:59.999 (Date)
4半期(3月決算)を前提とした値(計算)となっています。
時刻も最後 23:59:59.999 を指定しています。

10. 00:00:00の日時を取得 (Date)

getFirstTimeOfDay(date: Date): Date {
    let firstDatetime = new Date(date);
    firstDatetime.setHours(0, 0, 0, 0);
    return firstDatetime;
}

例)2022/02/07 13:00:00 (Date) ⇒ 2022/02/07 00:00:00.000 (Date)
setHours は (時, 分, 秒, ミリ秒) なので、他の時刻にしたい場合ここを変更すればOKです。