[learning javascript] chapter 15. 날짜와 시간

날짜와 시간

날짜, 타임존, 타임스탬프, 유닉스 시간

Date 객체 만들기

// 자바스크립트의 월은 0으로 시작(0이 1월) new Date(2018, 0); // 2018년 1월 1일 0시 new Date(2018, 1); // 2018년 2월 1일 0시 new Date(2018, 1, 14); // 2018년 2월 14일 0시 new Date(2018, 1, 14, 13); // 2018년 2월 14일 13시 new Date(2018, 1, 14, 13, 30); // 2018년 2월 14일 13시 30분 new Date(2018, 1, 14, 13, 30, 5); // 2018년 2월 14일 13시 30분 5초 new Date(2018, 1, 14, 13, 30, 5, 500); // 2018년 2월 14일 13시 30분 5.5초

// 유닉스 타임스탬프 new Date(0); // 1970년 1월 1일 12시 new Date(1000); // 1970년 1월 1일 12시 0분 1초 new Date(1463443200000); // 2016년 5월 16일 17시

// 유닉스 시간 원점 이전의 날짜 구할 때 new Date(-36524606060*1000); // 1969년 1월 1일 12시

// 날짜 문자열 해석(표준시 기준) new Date(‘June 14, 1903’); // 1903년 6월 14일 12시 (지역표준시) new Date(‘June 14, 1903 GMT-0000’); // 1903년 6월 14일 12시 (UTC)


## Moment.js
- 타임존을 지원하는 버전과 지원하지 않는 버전 두 가지가 있음
- CDN을 통해 불러올 수 있음
```html
<script src="//cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.4.0/moment-timezone.min.js"></script>

날짜 데이터 만들기

서버에서 날짜 생성하기

날짜 데이터 전송하기

날짜 형식

// 로스엔젤레스 기준 d.toLocaleDateString() // “5/9/1930” d.toLocaleFormat() // “5/9/1930 4:00:00 PM” d.toLocaleTimeString() // “4:00:00 PM” d.toTimeString() // “17:00:00 GMT-0700 (Pacific Daylight Time)” d.toUTCString() // “Sat, 10 May 1930, 00:00:00 GMT”

moment(d).format(“YYYY-MM-DD”); // “1930-05-09” moment(d).format(“YYYY-MM-DD HH:mm”); // “1930-05-09 17:00” moment(d).format(“YYYY-MM-DD HH:mm Z”); // “1930-05-09 17:00 -07:00” moment(d).format(“YYYY-MM-DD HH:mm [UTC]Z”); // “1930-05-09 17:00 UTC-07:00” moment(d).format(“YYYY년 M월 D일 HH:mm”); // “1930년 5월 10일 09:00”

moment(d).format(“dddd, MMMM [the] Do, YYYY”); // “Fridday, May the 9th, 1930”

moment(d).format(“h:mm a”); // “5:00 PM”


## 날짜 구성 요소
- Date인스턴스의 각 구성요소에 접근할 때는 다음과 같이 사용
```javascript
const d = new Date(Date.UTC(1815, 9, 10));

// 로스엔젤레스 기준
d.getFullYear()     // 1815
d.getMonth()        // 9 (10월)
d.getDate()         // 9
d.getDay()          // 1 (월요일)
d.getHours()        // 17
d.getMinutes()      // 0
d.getSeconds()      // 0 
d.getMilliseconds() // 0

// UTC 기준 메소드
d.getUTCFullYear()  // 1815
d.getUTCMonth()     // 9 (10월)
d.getUTCDate()      // 10
// ...etc

날짜 비교

d1 > d2 // false d1 < d2 // true


## 날짜 연산
- 날짜에서 날짜를 빼면 몇 밀리초가 지났는 지 알 수 있음
```javascript
const msDiff =  d2 - d1;                    // 417740400000 ms
const daysDiff = msDiff/1000/60/60/24;      // 4834.96 days

// 랜덤한 날짜를 몇 개 만듦 const min = new Date(2017, 0, 1).valueOf(); const delta = new Date(2020, 0, 1).valueOf() - min; for(let i=0; i<10; i++) dates.push(new Date(min + delta*Math.random()));

// dates 배열은 랜덤으로 만들었으므로 뒤죽박죽일 것임 // 다음과 같이 역순으로 정렬 할 수 있음 dates.sort((a,b) => b - a);

// 날짜순으로 정렬할 수도 있음 dates.sort((a, b) => a - b);

- `Moment.js`에는 날짜를 빼거나 더하는 데 유용한 메서드도 많이 들어 있음
```javascript
let m = moment();           // 현재
m.add(3, 'days');           // m은 이제 3일 뒤
m.subtract(2, 'years');     // m은 이제 2년 전으로부터 3일이 지난 날짜입니다.

m = moment();               // 리셋
m.startOf('year');          // m은 이제 올해의 1월 1일
m.endOf('month');           // m은 이제 올해의 1월 31일

사용자가 알기 쉬운 상대적 날짜

요약