JavaScriptの「Date.getDate」メソッドを徹底解説! 使い方からサンプルコードまで


JavaScript における "Date" オブジェクトは、特定の時点または日付を表すために使用されます。現在の日付と時刻、過去の日付と時刻、未来の日付と時刻など、さまざまな日付と時刻を表現できます。

"Date" オブジェクトは、さまざまなメソッドを提供しており、日付と時刻に関する情報へのアクセスと操作を可能にします。これらのメソッドには、以下のものがあります。

  • toJSON(): JSON 形式の文字列に変換します。
  • toLocaleString(): 日付と時刻をロケールに基づいた文字列に変換します。
  • toDateString(): 日付を文字列に変換します。
  • toString(): 日付と時刻を文字列に変換します。
  • setMilliseconds(): ミリ秒を設定します。
  • setSeconds(): 秒を設定します。
  • setMinutes(): 分を設定します。
  • setHours(): 時を設定します。
  • setFullYear(): 年を設定します。
  • setMonth(): 月を設定します。
  • setDate(): 日付を設定します。
  • getTime(): 1970 年 1 月 1 日 00:00:00 UTC からの経過ミリ秒数を取得します。
  • getMilliseconds(): ミリ秒を取得します。
  • getSeconds(): 秒を取得します。
  • getMinutes(): 分を取得します。
  • getHours(): 時を取得します。
  • getDate(): 日付を取得します。
  • getMonth(): 月を取得します。
  • getFullYear(): 年を取得します。

"getDate" メソッドとは?

"getDate" メソッドは、"Date" オブジェクトが表す日付の 日付 を取得するために使用されます。このメソッドは引数を取らず、1 から 31 までの整数を返します。返される値は、月の 1 日から最後の日までとなります。

"getDate" メソッドの例

// 現在の日付を取得
const today = new Date();

// 現在の日付の「日」を取得
const day = today.getDate();

// コンソールに「日」を出力
console.log(day);

この例では、まず new Date() を使用して現在の日付を表す "Date" オブジェクトを作成します。次に、getDate() メソッドを使用して、そのオブジェクトの日付を取得します。最後に、console.log() を使用して、取得した日付をコンソールに出力します。

// 特定の日付を文字列で指定して「日」を取得
const birthday = new Date('1980-07-20');
const birthdayDay = birthday.getDate();
console.log(birthdayDay); // 20

// ミリ秒数で指定して「日」を取得
const timestamp = 1657516800000;
const dateFromTimestamp = new Date(timestamp);
const dayFromTimestamp = dateFromTimestamp.getDate();
console.log(dayFromTimestamp); // 13

これらの例は、getDate メソッドを使用して、さまざまな方法で日付を取得する方法を示しています。



// 2024年8月10日の「日」を取得
const specificDate = new Date(2024, 7, 10);
const specificDay = specificDate.getDate();
console.log(specificDay); // 10

ミリ秒数から「日」を取得

この例では、Date.fromMillis() 関数と getDate() メソッドを使用して、ミリ秒数から「日」を取得する方法を示します。

// 1657516800000 ミリ秒から「日」を取得
const timestamp = 1657516800000;
const dateFromTimestamp = Date.fromMillis(timestamp);
const dayFromTimestamp = dateFromTimestamp.getDate();
console.log(dayFromTimestamp); // 13

経過日数を出力

この例では、Date.now() 関数と getDate() メソッドを使用して、現在の日付と特定の日付の差を求め、経過日数を出力する方法を示します。

// 基準となる日付
const基準日付 = new Date(2024, 0, 1);

// 現在の日付
const today = new Date();

// 経過日数の計算
const diffInMs = today.getTime() -基準日付.getTime();
const diffInDays = diffInMs / (1000 * 60 * 60 * 24);

// 経過日数の出力
console.log(diffInDays); // 184 (2024年6月13日時点)

特定の月の最初の日と最後の日を取得

この例では、getDate() メソッドを使用して、特定の月の最初の日と最後の日を取得する方法を示します。

// 2024年8月の最初の日と最後の日を取得
const month = 7; // 8 = 8月

// 最初の日の「日」
const firstDayOfMonth = new Date(2024, month, 1);
const firstDay = firstDayOfMonth.getDate();
console.log(`2024年8月の最初の日: ${firstDay}`); // 1

// 最後の日を取得
const lastDateOfMonth = new Date(2024, month + 1, 0);
const lastDayOfMonth = lastDateOfMonth.getDate();
console.log(`2024年8月の最後の日: ${lastDayOfMonth}`); // 31

特定の曜日の最初の「日」を取得

この例では、getDate() メソッドと getDay() メソッドを使用して、特定の曜日の最初の「日」を取得する方法を示します。

// 曜日 (0 = 日曜日、1 = 月曜日、...)
const targetDay = 1; // 1 = 月曜日

// 特定の曜日の最初の月の「日」
const firstDayOfTargetDay = new Date(2024, 0, 1);
let firstDay = firstDayOfTargetDay.getDate();
while (firstDayOfTargetDay.getDay() !== targetDay) {
  firstDay++;
  firstDayOfTargetDay = new Date(2024, 0, firstDay);
}
console.log(`2024年1月の最初の月曜日: ${firstDay}`); // 1

// 特定の曜日の最後の月の「日」
const lastDateOfMonth = new Date(2024, 0, 31);
let lastDay = lastDateOfMonth.getDate();
while (lastDateOfMonth.getDay() !== targetDay) {
  lastDay--;
  lastDateOfMonth = new Date(2024, 0, lastDay);
}
console.log(`2024年1月の最後の月曜日: ${lastDay}`); // 29


  1. Using Destructuring

    JavaScript's destructuring syntax provides a concise way to extract properties from objects. In the case of a Date object, you can destructure the getDate() value directly:

    const today = new Date();
    const [day] = [today.getDate()];
    console.log(day); // Output: 13 (assuming today's date is June 13, 2024)
    
  2. Using Template Literals

    Template literals offer a convenient way to embed expressions within strings. You can utilize a template literal to directly access the getDate() value:

    const today = new Date();
    const currentDay = `Today's date is the ${today.getDate()}th of June, 2024`;
    console.log(currentDay); // Output: Today's date is the 13th of June, 2024
    
  3. Using Arrow Functions

    Arrow functions provide a concise way to define functions inline. You can create an arrow function that returns the getDate() value:

    const today = new Date();
    const getDay = (date) => date.getDate();
    const currentDay = getDay(today);
    console.log(currentDay); // Output: 13