JavaScriptの日付取得メソッド
これらのメソッドは、日付オブジェクトから情報を取得するために使用できます。
Method | Description |
---|---|
getFullYear() | Get the year as a four digit number (yyyy) |
getMonth() | Get the month as a number (0-11) |
getDate() | Get the day as a number (1-31) |
getHours() | Get the hour (0-23) |
getMinutes() | Get the minute (0-59) |
getSeconds() | Get the second (0-59) |
getMilliseconds() | Get the millisecond (0-999) |
getTime() | Get the time (milliseconds since January 1, 1970) |
getDay() | Get the weekday as a number (0-6) |
Date.now() | Get the time. ECMAScript 5. |
getTime()メソッド
このgetTime()
メソッドは、1970年1月1日からのミリ秒数を返します。
例
const d = new Date();
d.getTime();
getFullYear()メソッド
このgetFullYear()
メソッドは、日付の年を4桁の数値として返します。
例
const d = new Date();
d.getFullYear();
getMonth()メソッド
このgetMonth()
メソッドは、日付の月を数値(0〜11)として返します。
例
const d = new Date();
d.getMonth();
JavaScriptでは、最初の月(1月)は月番号0であるため、12月は月番号11を返します。
名前の配列を使用してgetMonth()
、月を名前として返すことができます。
例
const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date();
let month = months[d.getMonth()];
getDate()メソッド
このgetDate()
メソッドは、日付の日を数値(1-31)として返します。
例
const d = new Date();
d.getDate();
getHours()メソッド
このgetHours()
メソッドは、日付の時間を数値(0〜23)として返します。
例
const d = new Date();
d.getHours();
getMinutes()メソッド
このgetMinutes()
メソッドは、日付の分を数値(0〜59)として返します。
例
const d = new Date();
d.getMinutes();
getSeconds()メソッド
このgetSeconds()
メソッドは、日付の秒を数値(0〜59)として返します。
例
const d = new Date();
d.getSeconds();
getMilliseconds()メソッド
このgetMilliseconds()
メソッドは、日付のミリ秒を数値(0〜999)として返します。
例
const d = new Date();
d.getMilliseconds();
getDay()メソッド
このgetDay()
メソッドは、日付の平日を数値(0〜6)として返します。
例
const d = new Date();
d.getDay();
JavaScriptでは、世界の一部の国では週の最初の日を「月曜日」と見なしている場合でも、週の最初の日(0)は「日曜日」を意味します。
名前の配列を使用してgetDay()
、曜日を名前として返すことができます。
例
const days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
const d = new Date();
let day = days[d.getDay()];
UTC日付メソッド
UTC日付メソッドは、UTC日付(協定世界時の日付)を操作するために使用されます。
Method | Description |
---|---|
getUTCDate() | Same as getDate(), but returns the UTC date |
getUTCDay() | Same as getDay(), but returns the UTC day |
getUTCFullYear() | Same as getFullYear(), but returns the UTC year |
getUTCHours() | Same as getHours(), but returns the UTC hour |
getUTCMilliseconds() | Same as getMilliseconds(), but returns the UTC milliseconds |
getUTCMinutes() | Same as getMinutes(), but returns the UTC minutes |
getUTCMonth() | Same as getMonth(), but returns the UTC month |
getUTCSeconds() | Same as getSeconds(), but returns the UTC seconds |
完全なJavaScript日付リファレンス
完全な日付の参照については、次のURLにアクセスしてください。
このリファレンスには、すべてのDateプロパティとメソッドの説明と例が含まれています。