Node.jsでサクッと実現!JavaScriptで2つの日付間のすべての日の配列を取得する方法
JavaScript で2つの日付間のすべてのの日付を取得する
このチュートリアルでは、JavaScriptとNode.jsを使用して、2つの指定された日付間のすべての日の配列を取得する方法を説明します。2つの方法を紹介します。
- ループを使用する: これは最も基本的な方法ですが、非効率になる可能性があります。
- モジュールを使用する: DateHelpersなどのモジュールを使用すると、コードをより簡潔で効率的にすることができます。
必要なもの
- JavaScript
- Node.js (モジュールを使用する場合)
方法 1: ループを使用する
function getDatesBetween(startDate, endDate) {
const dates = [];
let currentDate = startDate;
while (currentDate <= endDate) {
dates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
return dates;
}
const startDate = new Date(2020, 0, 1); // 2020-01-01
const endDate = new Date(2020, 11, 31); // 2020-12-31
const dates = getDatesBetween(startDate, endDate);
console.log(dates);
このコードは、startDate
とendDate
の間のすべての日の配列を返します。while
ループを使用して、currentDate
がendDate
を超えるまでループし、そのたびにdates
配列に新しいDate
オブジェクトを追加します。
const DateHelpers = require('date-helpers');
const startDate = new Date(2020, 0, 1);
const endDate = new Date(2020, 11, 31);
const dates = DateHelpers.getAllDatesBetween(startDate, endDate);
console.log(dates);
このコードは、DateHelpers
モジュールを使用して、startDate
とendDate
の間のすべての日の配列を返します。モジュールをインストールするには、次のコマンドを実行します。
npm install date-helpers
補足
- 上記のコードは、JavaScriptとNode.jsで動作します。
- 2つの日付が同じ場合、配列にはその日付のみが含まれます。
- 2番目の方法は、1番目の方法よりも効率的ですが、モジュールをインストールする必要があるという追加の要件があります。
上記以外にも、2つの日付間のすべての日の配列を取得する方法があります。ご自身のニーズに合った方法を選択してください。
function getDatesBetween(startDate, endDate) {
const dates = [];
let currentDate = startDate;
while (currentDate <= endDate) {
dates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
return dates;
}
const startDate = new Date(2023, 0, 1); // 2023-01-01
const endDate = new Date(2023, 11, 31); // 2023-12-31
const dates = getDatesBetween(startDate, endDate);
console.log(dates);
const DateHelpers = require('date-helpers');
const startDate = new Date(2023, 0, 1);
const endDate = new Date(2023, 11, 31);
const dates = DateHelpers.getAllDatesBetween(startDate, endDate);
console.log(dates);
説明
- 上記のコードは、
startDate
とendDate
の間のすべての日の配列を返します。 - 方法 2 は
DateHelpers
モジュールを使用して、より簡潔にコードを書いています。
実行方法
- 方法 1 を使用する場合は、コードを保存して
node
コマンドを使用して実行できます。 - 方法 2 を使用する場合は、まず
DateHelpers
モジュールをインストールする必要があります。次のコマンドを実行します。
npm install date-helpers
出力
[
Mon Jan 01 2024 00:00:00 GMT-0800 (Pacific Standard Time),
Tue Jan 02 2024 00:00:00 GMT-0800 (Pacific Standard Time),
Wed Jan 03 2024 00:00:00 GMT-0800 (Pacific Standard Time),
...
Fri Dec 28 2024 00:00:00 GMT-0800 (Pacific Standard Time),
Sat Dec 29 2024 00:00:00 GMT-0800 (Pacific Standard Time),
Sun Dec 30 2024 00:00:00 GMT-0800 (Pacific Standard Time),
Mon Dec 31 2024 00:00:00 GMT-0800 (Pacific Standard Time)
]
- 上記のコードはあくまで例であり、ニーズに合わせて変更できます。
JavaScript で 2 つの日付間のすべての日の配列を取得するその他の方法
Array.from() と Date.prototype.setDate() を使用する
この方法は、Array.from()
メソッドを使用して、指定された範囲の整数を配列に変換し、その後 Date.prototype.setDate()
メソッドを使用して各要素を新しい Date
オブジェクトに変換します。
function getDatesBetween(startDate, endDate) {
const dates = Array.from({ length: endDate.getDate() - startDate.getDate() + 1 }, (_, i) => new Date(startDate.setDate(startDate.getDate() + i)));
return dates;
}
const startDate = new Date(2023, 0, 1);
const endDate = new Date(2023, 11, 31);
const dates = getDatesBetween(startDate, endDate);
console.log(dates);
ジェネレータを使用する
この方法は、ジェネレータを使用して、2 つの日付間のすべての日のイテレータを作成します。
function* getDatesBetween(startDate, endDate) {
let currentDate = startDate;
while (currentDate <= endDate) {
yield new Date(currentDate);
currentDate.setDate(currentDate.getDate() + 1);
}
}
const startDate = new Date(2023, 0, 1);
const endDate = new Date(2023, 11, 31);
const dates = [...getDatesBetween(startDate, endDate)];
console.log(dates);
ライブラリを使用する
Moment.js や Lodash などのライブラリには、2 つの日付間のすべての日の配列を取得するためのユーティリティ関数が含まれている場合があります。
// Moment.js を使用する
const moment = require('moment');
const startDate = moment('2023-01-01');
const endDate = moment('2023-12-31');
const dates = moment.range(startDate, endDate).by('day').toArray();
console.log(dates);
// Lodash を使用する
const _ = require('lodash');
const startDate = new Date(2023, 0, 1);
const endDate = new Date(2023, 11, 31);
const dates = _.times(endDate.getDate() - startDate.getDate() + 1, (i) => new Date(startDate.setDate(startDate.getDate() + i)));
console.log(dates);
使用する方法は、ニーズと好みによって異なります。ループを使用する方法は最も基本的な方法ですが、非効率になる可能性があります。モジュールを使用する方法は、コードをより簡潔で効率的にすることができます。ジェネレータを使用する方法は、メモリ効率が高く、大きな日付範囲を処理する場合に適しています。ライブラリを使用する方法は、便利なユーティリティ関数を提供しますが、追加のライブラリをインストールする必要があるという追加の要件があります。
javascript node.js date