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?

UTCの深夜(00:00:00:00Z)を取得する関数

0
Last updated at Posted at 2026-03-30

0. はじめに

この記事は、学んだ技術や知識についてを備忘録としてまとめることを目的にしている。今回はUTC(世界協定時刻)の深夜の時間をいくつかのケースで取得する関数をまとめる。

1. 開発環境

  • OS: macOS Sequoia 15.5
  • エディタ: VS Code
  • ライブラリ: React 19.2.0
  • フレームワーク: Next.js 16.0.5
    • ルーター: App Router

2. 基本実装

プロジェクト作成方法

ターミナルにてプロジェクトを作成したいディレクトリ内に移動して、以下のコマンドを実行。

ターミナル
# プロジェクトの雛形を作成
npx create-next-app

プロジェクト作成時の質問は以下の通り。

スクリーンショット 2025-11-29 18.39.53.png

作成が完了したら以下のコマンドで起動

npm run dev

2.1. 汎用関数の作成

基準日からの相対的なUTC深夜(00:00:00Z)を取得
/** 
 * 指定した基準日からの相対的なUTC深夜(00:00:00Z)を取得する
 * * @param {Object} options
 * @param {number} [options.yearOffset=0] - 年の加算
 * @param {number} [options.monthOffset=0] - 月の加算
 * @param {number} [options.dayOffset=0] - 日の加算
 * @param {boolean} [options.resetToFirstDay=false] - 日を1日にリセットするか
 * @param {boolean} [options.resetToFirstMonth=false] - 月を1月にリセットするか
 * @returns {string} ISO 8601形式の文字列
 */

export const getRelativeMidnightUTC = ({
  yearOffset = 0,
  monthOffset = 0,
  dayOffset = 0,
  resetToFirstDay = false,
  resetToFirstMonth = false,
} = {}) => {
  // 基準となる現在の日時を取得
  const now = new Date();

  // 基準日時からの相対的な日時を計算
  const year = now.getFullYear() + yearOffset;
  const month = resetToFirstMonth ? 0 : now.getMonth() + monthOffset;
  const date = resetToFirstDay ? 1 : now.getDate() + dayOffset;

  const utcMs = Date.UTC(year, month, date);

  return new Date(utcMs).toISOString();
}

2.2. 実装内容の解説

2.2.1. Date()

Dateオブジェクトを生成し、現在時刻を表す文字列を返す。

Date()
console.log(new Date());

// 実行結果
// Mon Mar 30 2026 21:56:37 GMT+0900 (日本標準時)

2.2.2. getFullYear()/getMonth()/getDate()

Dateオブジェクトからそれぞれ年・月・日の要素を返す。

getFullYear()/getMonth()/getDate()
console.log(new Date());
console.log(new Date().getFullYear());
console.log(new Date().getMonth());
console.log(new Date().getDate());

// 実行結果
// Mon Mar 30 2026 22:01:01 GMT+0900 (日本標準時)
// 2026
// 2
// 30

2.2.3. UTC(year, month, day)

指定して年・月・日のUTC(協定世界時)における「1970年1月1日00:00:00」からの経過ミリ秒数を計算して返す。

時・分・秒の引数を省略すると自動的に00:00:00として計算される。
月は0から始まり、これが1月を返すため注意。

UT(year, month, day)
// 比較用
console.log(new Date(2026, 2, 30));
console.log(Date.UTC(2026, 2, 30));

// 実行結果
// Mon Mar 30 2026 00:00:00 GMT+0900 (日本標準時)
// 1774828800000

2.2.4. toISOString();

Dateオブジェクトを、ISO 8601形式の文字列に変換して返す。

toISOString()
console.log(new Date(2026, 2, 30));
console.log(Date.UTC(2026, 2, 30));
console.log(new Date(Date.UTC(2026, 2, 30)).toISOString());

// 実行結果
// Mon Mar 30 2026 00:00:00 GMT+0900 (日本標準時)
// 1774828800000
// 2026-03-30T00:00:00.000Z

3. 実行例

3.1. 今日の0時(UTC)

今日の0時(UTC)
console.log(getRelativeMidnightUTC());
// 実行結果
// 2026-03-30T00:00:00.000Z

3.2. 明日の0時 (UTC)

明日の0時 (UTC)
console.log(getRelativeMidnightUTC({ dayOffset: 1 }));
// 実行結果
// 2026-03-31T00:00:00.000Z

3.3. 今月の1日 (UTC)

今月の1日 (UTC)
console.log(getRelativeMidnightUTC({ resetToFirstDay: true }));
// 実行結果
// 2026-03-01T00:00:00.000Z

3.4. 翌月の1日 (UTC)

翌月の1日 (UTC)
console.log(getRelativeMidnightUTC({ monthOffset: 1, resetToFirstDay: true }));
// 実行結果
// 2026-04-01T00:00:00.000Z

3.5. 今年の1月1日 (UTC)

今年の1月1日 (UTC)
console.log(getRelativeMidnightUTC({ resetToFirstMonth: true, resetToFirstDay: true }));
// 実行結果
// 2026-01-01T00:00:00.000Z

3.6. 翌年の1月1日 (UTC)

翌年の1月1日 (UTC)
console.log(getRelativeMidnightUTC({ yearOffset: 1, resetToFirstMonth: true, resetToFirstDay: true }));
// 実行結果
// 2027-01-01T00:00:00.000Z

参考資料

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?