[JS] Date
55242 ワード
Dateオブジェクトは、日付と時間(年、月、日、時、分、秒、ミリ秒(千分の一ミリ秒、ms)のメソッドを提供するコンストラクション関数です.
Date作成者関数によって作成されたDateオブジェクトには、内部に数値値があります.デフォルトでは、現在の日付と時刻を表す整数値があります.
->この値は、1970年1月1日00:00(UTC)から現在の時間までのミリ秒を表します.
統一協調時間(UTC):世界協定時、国際標準時
KST(韓国標準時):韓国標準時
現在の日付と時間はjavascriptコードが実行するシステムクロックによって決定されます.システムクロックの設定(timezone、time)によって、異なる値を持つことができます.
パラメータを渡さない場合は、現在の日付と時間を持つインスタンスが返されます.
-転送数が年、月、日、時、分、秒、およびミリ秒を表す場合、指定した日付および時間を持つインスタンスが返されます.この場合、年、月は指定しなければなりません.指定されていないオプション情報は0または1に初期化されます.
0~59の間の整数秒、0~59の間の整数ミリ秒、0~59の間の整数ミリ秒、0~開始、0=1月の間の整数時間(0~31の間は0~1)、1900年以降の年の月を表します.
年、月が指定されていない場合は、1970年1月1日00:00(UTC)のインスタンスが返されます.
年度を示す4桁を返します. 年度を示す4桁を設定します.年度以外の月、日も設定できます. get:月を表す0から11の整数を返します.1月は0、12月は11です. set:月を表す0~11の整数を設定します.1月は0、12月は11です.月以外の日付も設定できます.
は、の日付(1~31)を表す整数を返します. の日付(1~31)を表す整数を設定します.
曜日(0~6)を表す整数を返します.戻り値は次のとおりです. 日曜日の戻り値日曜日0月曜日火曜日火曜日水曜日3星期木曜日金曜日5土曜日6 get:時間(0から23)を表す整数を返します. set:時間(0~23)を表す整数を設定します.時間以外の分、秒、ミリ秒を設定することもできます. get:分(0から59)を表す整数を返します. set:分(0~59)を表す整数を設定します.分以外にもミリ秒を設定できます. get:秒(0から59)を表す整数を返します. set:秒(0~59)を表す整数を設定します.秒以外にもミリ秒を設定できます. ミリ秒(0~999)を表す整数を返して設定します.
1970年1月1日00:00:00(UTC)を起点として、現在の時間が経過したミリ秒を返して設定します. UTCと指定領域設定時間の差を分単位で返します. 人の読み取り可能な形式で日付を返します. 人の読み取り可能な形で時間を返します. ISO形式でデータオブジェクト時間を表す文字列 を返す.引数をローカル基準として、Dateオブジェクトの日付と時刻を文字列で表して返します. パラメータで渡されたローカル基準は、Dateオブジェクトの時間を文字列で表し、返されます.
現在の日付と時刻を秒単位で繰り返し出力する例.
『モダンjavascript deepdiveと後続探索』
Date作成者関数によって作成されたDateオブジェクトには、内部に数値値があります.デフォルトでは、現在の日付と時刻を表す整数値があります.
->この値は、1970年1月1日00:00(UTC)から現在の時間までのミリ秒を表します.
統一協調時間(UTC):世界協定時、国際標準時
KST(韓国標準時):韓国標準時
現在の日付と時間はjavascriptコードが実行するシステムクロックによって決定されます.システムクロックの設定(timezone、time)によって、異なる値を持つことができます.
📆 new Date()
パラメータを渡さない場合は、現在の日付と時間を持つインスタンスが返されます.
const date = new Date();
console.log(date); // Thu May 16 2019 17:16:13 GMT+0900 (한국 표준시)
// new Date(milliseconds)
// 인수로 숫자 타입의 밀리초를 전달하면 1970년 1월 1일 00: 00(UTC)을 기점으로 인수로 전달된 밀리초만큼 경과한 날짜와 시간을 가지는 인스턴스를 반환한다.
// KST(Korea Standard Time)는 GMT(그리니치 평균시: Greenwich Mean Time)에 9시간을 더한 시간이다.
let date = new Date(0);
console.log(date); // Thu Jan 01 1970 09:00:00 GMT+0900 (한국 표준시)
// 86400000ms는 1day를 의미한다.
// 1s = 1,000ms
// 1m = 60s * 1,000ms = 60,000ms
// 1h = 60m * 60,000ms = 3,600,000ms
// 1d = 24h * 3,600,000ms = 86,400,000ms
date = new Date(86400000);
console.log(date); // FFri Jan 02 1970 09:00:00 GMT+0900 (한국 표준시)
// new Date(dateString)
// 인수로 날짜와 시간을 나타내는 문자열을 전달하면 지정된 날짜와 시간을 가지는 인스턴스를 반환한다.이때 인수로 전달한 문자열은 Date.parse 메소드에 의해 해석 가능한 형식이어야 한다.
let date = new Date('May 16, 2019 17:22:10');
console.log(date); // Thu May 16 2019 17:22:10 GMT+0900 (한국 표준시)
date = new Date('2019/05/16/17:22:10');
console.log(date); // Thu May 16 2019 17:22:10 GMT+0900 (한국 표준시)
⏲️ new Date(year, month[, day, hour, minute, second, millisecond])
-転送数が年、月、日、時、分、秒、およびミリ秒を表す場合、指定した日付および時間を持つインスタンスが返されます.この場合、年、月は指定しなければなりません.指定されていないオプション情報は0または1に初期化されます.
0~59の間の整数秒、0~59の間の整数ミリ秒、0~59の間の整数ミリ秒、0~開始、0=1月の間の整数時間(0~31の間は0~1)、1900年以降の年の月を表します.
年、月が指定されていない場合は、1970年1月1日00:00(UTC)のインスタンスが返されます.
// 월을 나타내는 4는 5월을 의미한다.
// 2019/5/1/00:00:00:00
new Date(2019, 4);
// Wed May 01 2019 00:00:00 GMT+0900 (한국 표준시)
// 월을 나타내는 4는 5월을 의미한다.
// 2019/5/16/17:24:30:00
new Date(2019, 4, 16, 17, 24, 30, 0);
// Thu May 16 2019 17:24:30 GMT+0900 (한국 표준시)
// 가독성이 훨씬 좋다.
new Date('2019/5/16/17:24:30:10');
// Thu May 16 2019 17:24:30 GMT+0900 (한국 표준시)
📆 Date Method
⏲️ getFullYear
const today = new Date();
const year = today.getFullYear();
console.log(today); // Thu May 16 2019 17:39:30 GMT+0900 (한국 표준시)
console.log(year); // 2019
```]]
⏲️ setFullYear
dateObj.setFullYear(year[, month[, day]])
const today = new Date();
// 년도 지정
today.setFullYear(2000);
let year = today.getFullYear();
console.log(today); // Tue May 16 2000 17:42:40 GMT+0900 (한국 표준시)
console.log(year); // 2000
// 년도 지정
today.setFullYear(1900, 0, 1);
year = today.getFullYear();
console.log(today); // Mon Jan 01 1900 17:42:40 GMT+0827 (한국 표준시)
console.log(year); // 1900
⏲️ getMonth & setMonth
const today = new Date();
const month = today.getMonth();
console.log(today); // 2021-12-09T04:19:58.314Z
console.log(month); // 11
dateObj.setMonth(month[, day])
const today = new Date();
// 월을 지정
today.setMonth(0); // 1월
let month = today.getMonth();
console.log(today); // Wed Jan 16 2019 17:45:20 GMT+0900 (한국 표준시)
console.log(month); // 0
// 월/일을 지정
today.setMonth(11, 1); // 12월 1일
month = today.getMonth();
console.log(today); // Sun Dec 01 2019 17:45:20 GMT+0900 (한국 표준시)
console.log(month); // 11
⏲️ getDate
は、
const today = new Date();
const date = today.getDate();
console.log(today); // Thu May 16 2019 17:46:42 GMT+0900 (한국 표준시)
console.log(date); // 16
⏲️ setDate
const today = new Date();
// 날짜 지정
today.setDate(1);
const date = today.getDate();
console.log(today); // Wed May 01 2019 17:47:01 GMT+0900 (한국 표준시)
console.log(date); // 1
⏲️ getDay
曜日
const today = new Date();
const day = today.getDay();
console.log(today); // 2021-12-09T04:22:37.467Z
console.log(day); // 4
⏲️ getHours & setHours
const today = new Date();
const hours = today.getHours();
console.log(today); // Thu May 16 2019 17:48:03 GMT+0900 (한국 표준시)
console.log(hours); // 17
dateObj.setHours(hour[, minute[, second[, ms]]])
const today = new Date();
// 시간 지정
today.setHours(7);
let hours = today.getHours();
console.log(today); // Thu May 16 2019 07:49:06 GMT+0900 (한국 표준시)
console.log(hours); // 7
// 시간/분/초/밀리초 지정
today.setHours(0, 0, 0, 0); // 00:00:00:00
hours = today.getHours();
console.log(today); // Thu May 16 2019 00:00:00 GMT+0900 (한국 표준시)
console.log(hours); // 0
⏲️ getMinutes & setMinutes
Date('2021/12/09/13:28').getMinutes()
dateObj.setMinutes(minute[, second[, ms]])
const today = new Date();
// 분 지정
today.setMinutes(50);
let minutes = today.getMinutes();
console.log(today); // Thu May 16 2019 17:50:30 GMT+0900 (한국 표준시)
console.log(minutes); // 50
// 분/초/밀리초 지정
today.setMinutes(5, 10, 999); // HH:05:10:999
minutes = today.getMinutes();
console.log(today); // Thu May 16 2019 17:05:10 GMT+0900 (한국 표준시)
console.log(minutes); // 5
⏲️ getSeconds & setSeconds
const today = new Date();
const seconds = today.getSeconds();
console.log(today); // Thu May 16 2019 17:53:17 GMT+0900 (한국 표준시)
console.log(seconds); // 17
dateObj.setSeconds(second[, ms])
const today = new Date();
// 초 지정
today.setSeconds(30);
let seconds = today.getSeconds();
console.log(today); // Thu May 16 2019 17:54:30 GMT+0900 (한국 표준시)
console.log(seconds); // 30
// 초/밀리초 지정
today.setSeconds(10, 0); // HH:MM:10:000
seconds = today.getSeconds();
console.log(today); // Thu May 16 2019 17:54:10 GMT+0900 (한국 표준시)
console.log(seconds); // 10
⏲️ getMilliseconds & setMilliseconds
const today = new Date();
const ms = today.getMilliseconds();
console.log(today); // Thu May 16 2019 17:55:02 GMT+0900 (한국 표준시)
console.log(ms); // 905
const today = new Date();
// 밀리초 지정
today.setMilliseconds(123);
const ms = today.getMilliseconds();
console.log(today); // Thu May 16 2019 17:55:45 GMT+0900 (한국 표준시)
console.log(ms); // 123
⏲️ getTime & setTime
1970年1月1日00:00:00(UTC)を起点として、現在の時間が経過したミリ秒を返して設定します.
const today = new Date();
const time = today.getTime();
console.log(today); // Thu May 16 2019 17:56:08 GMT+0900 (한국 표준시)
console.log(time); // 1557996968335
dateObj.setTime(time)
const today = new Date();
// 1970년 1월 1일 00:00:00(UTC)를 기점으로 현재 시간까지 경과된 밀리초 지정
today.setTime(86400000); // 86400000 === 1day
const time = today.getTime();
console.log(today); // Fri Jan 02 1970 09:00:00 GMT+0900 (한국 표준시)
console.log(time); // 86400000
⏲️ getTimezoneOffset
const today = new Date();
const x = today.getTimezoneOffset() // 60; // -9
console.log(today); // Thu May 16 2019 17:58:13 GMT+0900 (한국 표준시)
console.log(x); // -9
KST(Korea Standard Time)는 UTC에 9시간을 더한 시간이다. 즉, UTC = KST - 9h이다.
⏲️ toDateString
const d = new Date('2019/5/16/18:30');
console.log(d.toString()); // Thu May 16 2019 18:30:00 GMT+0900 (한국 표준시)
console.log(d.toDateString()); // Thu May 16 2019
⏲️ toTimeString
const d = new Date('2019/5/16/18:30');
console.log(d.toString()); // Thu May 16 2019 18:30:00 GMT+0900 (한국 표준시)
console.log(d.toTimeString()); // 18:30:00 GMT+0900 (한국 표준시)
⏲️ toISOString
const d = new Date();
console.log(d.toString()); // Thu May 16 2019 18:30:00 GMT+0900 (한국 표준시)
console.log(d.toISOString()); // 2021-12-09T04:46:50.299Z
console.log(d.toISOString().slice(0, 10)); //2021-12-09
⏲️ toLocalString & toLocalTimeString
const today = new Date('2021/12/09/13:53);
today.toString();
console.log(today.toLocaleString('ko-KR')); //2021. 12. 9. 오후 1:53:00
console.log(today.toLocaleString('en-US')); //12/9/2021, 1:53:00 PM
console.log(today.toLocaleTimeString('ko-KR')); //오후 1:53:00
console.log(today.toLocaleTimeString('en-US')); //1:53:00 PM
📆 Date Exampleの利用
現在の日付と時刻を秒単位で繰り返し出力する例.
(function printNow() {
const today = new Date();
const dayNames = ['(일요일)', '(월요일)', '(화요일)', '(수요일)', '(목요일)', '(금요일)', '(토요일)'];
// getDay: 해당 요일(0 ~ 6)를 나타내는 정수를 반환한다.
const day = dayNames[today.getDay()];
const year = today.getFullYear();
const month = today.getMonth() + 1;
const date = today.getDate();
let hour = today.getHours();
let minute = today.getMinutes();
let second = today.getSeconds();
const ampm = hour >= 12 ? 'PM' : 'AM';
// 12시간제로 변경
hour %= 12;
hour = hour || 12; // 0 => 12
// 10미만인 분과 초를 2자리로 변경
minute = minute < 10 ? '0' + minute : minute;
second = second < 10 ? '0' + second : second;
const now = `${year}년 ${month}월 ${date}일 ${day} ${hour}:${minute}:${second} ${ampm}`;
//1초마다 printnow 함수를 재귀 호출한다.
console.log(now);
setTimeout(printNow, 1000);
}());
『モダンjavascript deepdiveと後続探索』
Reference
この問題について([JS] Date), 我々は、より多くの情報をここで見つけました https://velog.io/@songjy377/JS-Dateテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol