Skip to content

Commit 98831a5

Browse files
docs(blog): SEO 기술 팁 포스트 추가 - JavaScript Temporal API (3개 언어)
1 parent b6f68d1 commit 98831a5

3 files changed

Lines changed: 267 additions & 0 deletions
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
layout: post
3+
title: "JavaScript Temporal API - Finally a Proper Date Replacement"
4+
date: 2026-02-07 09:00:00 +0900
5+
categories: [Development, Tips]
6+
tags: [JavaScript, Temporal API, Date, TypeScript]
7+
author: "Kevin Park"
8+
lang: en
9+
excerpt: "How to use the Temporal API to replace JavaScript's broken Date object with immutable, timezone-aware date handling"
10+
---
11+
12+
## Problem
13+
14+
JavaScript's `Date` object has frustrated developers for over two decades.
15+
16+
```javascript
17+
// Create January 15, 2026
18+
const date = new Date(2026, 0, 15); // month starts from 0?!
19+
console.log(date.getMonth()); // 0 ← It's January, but returns 0
20+
21+
// Mutating a reference changes the original
22+
const original = new Date(2026, 0, 15);
23+
const copy = original;
24+
copy.setMonth(5);
25+
console.log(original.getMonth()); // 5 ← original is mutated
26+
```
27+
28+
Zero-indexed months and mutable objects have been a constant source of bugs. Most teams ended up depending on libraries like moment.js or day.js just to handle dates sanely. Now there's a native solution.
29+
30+
## Solution
31+
32+
The `Temporal` API ships natively in Chrome 144 (January 2026) and Firefox 139 (May 2025). No libraries needed.
33+
34+
### Creating Dates
35+
36+
```javascript
37+
// PlainDate - date without time
38+
const date = Temporal.PlainDate.from('2026-02-07');
39+
const date2 = Temporal.PlainDate.from({ year: 2026, month: 2, day: 7 });
40+
41+
console.log(date.month); // 2 ← February is actually 2!
42+
console.log(date.dayOfWeek); // 6 (Saturday)
43+
44+
// PlainTime - time without date
45+
const time = Temporal.PlainTime.from('14:30:00');
46+
console.log(time.hour); // 14
47+
```
48+
49+
### Date Arithmetic
50+
51+
```javascript
52+
const today = Temporal.PlainDate.from('2026-02-07');
53+
54+
// 30 days later
55+
const later = today.add({ days: 30 });
56+
console.log(later.toString()); // 2026-03-09
57+
58+
// Original stays unchanged (immutable!)
59+
console.log(today.toString()); // 2026-02-07
60+
61+
// Duration between two dates
62+
const start = Temporal.PlainDate.from('2026-01-01');
63+
const end = Temporal.PlainDate.from('2026-02-07');
64+
const diff = start.until(end);
65+
console.log(diff.days); // 37
66+
```
67+
68+
### Timezone Conversion
69+
70+
```javascript
71+
// Current time in Seoul
72+
const now = Temporal.Now.zonedDateTimeISO('Asia/Seoul');
73+
console.log(now.toString());
74+
// 2026-02-07T14:30:00+09:00[Asia/Seoul]
75+
76+
// Convert to New York time
77+
const nyTime = now.withTimeZone('America/New_York');
78+
console.log(nyTime.toString());
79+
// 2026-02-07T00:30:00-05:00[America/New_York]
80+
```
81+
82+
Timezone conversion with `Date` required ugly workarounds. With Temporal, it's a single `withTimeZone()` call.
83+
84+
## Key Points
85+
86+
- **Months start from 1**: January = 1, December = 12. The most common off-by-one bug source is gone
87+
- **Immutable by design**: Every operation like `add()` and `subtract()` returns a new object. No more accidental mutations
88+
- **Purpose-built types**: `PlainDate` (date only), `PlainTime` (time only), `ZonedDateTime` (with timezone) — use exactly what you need
89+
- **Browser support**: Available in Chrome 144+ and Firefox 139+. For Safari, use `@js-temporal/polyfill` in production
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
layout: post
3+
title: "JavaScript Temporal APIで日付処理 - Dateオブジェクトはもう卒業"
4+
date: 2026-02-07 09:00:00 +0900
5+
categories: [Development, Tips]
6+
tags: [JavaScript, Temporal API, Date, TypeScript]
7+
author: "Kevin Park"
8+
lang: ja
9+
excerpt: "Dateオブジェクトの0始まりmonthやmutable問題をTemporal APIで解決する方法"
10+
---
11+
12+
## 問題
13+
14+
JavaScriptの`Date`オブジェクトは、20年以上開発者を悩ませてきました。
15+
16+
```javascript
17+
// 2026年1月15日を作りたい
18+
const date = new Date(2026, 0, 15); // monthが0から?!
19+
console.log(date.getMonth()); // 0 ← 1月なのに0が返ります
20+
21+
// 参照を変更すると元のオブジェクトも変わってしまう
22+
const original = new Date(2026, 0, 15);
23+
const copy = original;
24+
copy.setMonth(5);
25+
console.log(original.getMonth()); // 5 ← 元のオブジェクトが変更されました
26+
```
27+
28+
monthが0始まりであることと、mutableであることは常にバグの原因になっていました。結局moment.jsやday.jsなどのライブラリに頼ることが当たり前になっていましたが、ついにネイティブの解決策が登場しました。
29+
30+
## 解決方法
31+
32+
`Temporal` APIがChrome 144(2026年1月)とFirefox 139(2025年5月)からネイティブでサポートされています。ライブラリなしで日付を正しく扱えるようになりました。
33+
34+
### 日付の作成
35+
36+
```javascript
37+
// PlainDate - 時間なしの日付のみ
38+
const date = Temporal.PlainDate.from('2026-02-07');
39+
const date2 = Temporal.PlainDate.from({ year: 2026, month: 2, day: 7 });
40+
41+
console.log(date.month); // 2 ← ついに2月が2です!
42+
console.log(date.dayOfWeek); // 6(土曜日)
43+
44+
// PlainTime - 日付なしの時間のみ
45+
const time = Temporal.PlainTime.from('14:30:00');
46+
console.log(time.hour); // 14
47+
```
48+
49+
### 日付の計算
50+
51+
```javascript
52+
const today = Temporal.PlainDate.from('2026-02-07');
53+
54+
// 30日後
55+
const later = today.add({ days: 30 });
56+
console.log(later.toString()); // 2026-03-09
57+
58+
// 元のオブジェクトは変わりません(immutable!)
59+
console.log(today.toString()); // 2026-02-07
60+
61+
// 2つの日付の差分
62+
const start = Temporal.PlainDate.from('2026-01-01');
63+
const end = Temporal.PlainDate.from('2026-02-07');
64+
const diff = start.until(end);
65+
console.log(diff.days); // 37
66+
```
67+
68+
### タイムゾーン変換
69+
70+
```javascript
71+
// ソウル時間で現在時刻
72+
const now = Temporal.Now.zonedDateTimeISO('Asia/Seoul');
73+
console.log(now.toString());
74+
// 2026-02-07T14:30:00+09:00[Asia/Seoul]
75+
76+
// ニューヨーク時間に変換
77+
const nyTime = now.withTimeZone('America/New_York');
78+
console.log(nyTime.toString());
79+
// 2026-02-07T00:30:00-05:00[America/New_York]
80+
```
81+
82+
`Date`オブジェクトでタイムゾーン変換をするには複雑な処理が必要でしたが、Temporalなら`withTimeZone()`の一行で完了です。
83+
84+
## ポイント
85+
86+
- **monthが1から始まります**: 1月 = 1、12月 = 12。最も多いオフバイワンバグの原因がなくなりました
87+
- **immutable設計**: `add()``subtract()`などすべての操作は新しいオブジェクトを返します。意図しない変更が起きません
88+
- **用途別の型**: `PlainDate`(日付のみ)、`PlainTime`(時間のみ)、`ZonedDateTime`(タイムゾーン付き)— 必要なものだけ使えます
89+
- **ブラウザサポート**: Chrome 144+、Firefox 139+で利用可能です。Safariはまだなので、本番環境では`@js-temporal/polyfill`の併用をお勧めします
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
layout: post
3+
title: "JavaScript Temporal API로 날짜 다루기 - Date 객체는 이제 그만"
4+
date: 2026-02-07 09:00:00 +0900
5+
categories: [Development, Tips]
6+
tags: [JavaScript, Temporal API, Date, TypeScript]
7+
author: "Kevin Park"
8+
lang: ko
9+
excerpt: "Date 객체의 0부터 시작하는 month, mutable 문제를 Temporal API로 깔끔하게 해결하는 방법"
10+
---
11+
12+
## 문제
13+
14+
JavaScript의 `Date` 객체, 쓸 때마다 짜증나는 부분이 한두 개가 아니다.
15+
16+
```javascript
17+
// 2026년 1월 15일을 만들고 싶다
18+
const date = new Date(2026, 0, 15); // month가 0부터?!
19+
console.log(date.getMonth()); // 0 ← 1월인데 0이다
20+
21+
// 날짜를 바꿨더니 원본도 바뀐다
22+
const original = new Date(2026, 0, 15);
23+
const copy = original;
24+
copy.setMonth(5);
25+
console.log(original.getMonth()); // 5 ← 원본이 바뀌어버렸다
26+
```
27+
28+
month가 0부터 시작하는 건 20년 넘게 개발자들을 괴롭혀 온 문제다. 거기에 mutable이라 원본이 의도치 않게 바뀌는 버그까지. 결국 moment.js, day.js 같은 라이브러리를 가져다 쓰는 게 당연해졌는데, 이제 드디어 네이티브 해결책이 나왔다.
29+
30+
## 해결
31+
32+
`Temporal` API가 Chrome 144(2026년 1월), Firefox 139(2025년 5월)부터 기본 지원된다. 라이브러리 없이 날짜를 제대로 다룰 수 있게 된 거다.
33+
34+
### 날짜 생성
35+
36+
```javascript
37+
// PlainDate - 시간 없이 날짜만
38+
const date = Temporal.PlainDate.from('2026-02-07');
39+
const date2 = Temporal.PlainDate.from({ year: 2026, month: 2, day: 7 });
40+
41+
console.log(date.month); // 2 ← 드디어 2월이 2다!
42+
console.log(date.dayOfWeek); // 6 (토요일)
43+
44+
// PlainTime - 날짜 없이 시간만
45+
const time = Temporal.PlainTime.from('14:30:00');
46+
console.log(time.hour); // 14
47+
```
48+
49+
### 날짜 연산
50+
51+
```javascript
52+
const today = Temporal.PlainDate.from('2026-02-07');
53+
54+
// 30일 후
55+
const later = today.add({ days: 30 });
56+
console.log(later.toString()); // 2026-03-09
57+
58+
// 원본은 변하지 않는다 (immutable!)
59+
console.log(today.toString()); // 2026-02-07
60+
61+
// 두 날짜 사이 기간
62+
const start = Temporal.PlainDate.from('2026-01-01');
63+
const end = Temporal.PlainDate.from('2026-02-07');
64+
const diff = start.until(end);
65+
console.log(diff.days); // 37
66+
```
67+
68+
### 타임존 변환
69+
70+
```javascript
71+
// 한국 시간으로 현재 시각
72+
const now = Temporal.Now.zonedDateTimeISO('Asia/Seoul');
73+
console.log(now.toString());
74+
// 2026-02-07T14:30:00+09:00[Asia/Seoul]
75+
76+
// 뉴욕 시간으로 변환
77+
const nyTime = now.withTimeZone('America/New_York');
78+
console.log(nyTime.toString());
79+
// 2026-02-07T00:30:00-05:00[America/New_York]
80+
```
81+
82+
Date 객체로 타임존 변환하려면 온갖 삽질이 필요했는데, Temporal은 그냥 `withTimeZone()` 한 줄이면 끝이다.
83+
84+
## 핵심 포인트
85+
86+
- **month가 1부터 시작한다**: 1월 = 1, 12월 = 12. 20년 묵은 버그 원인이 사라졌다
87+
- **immutable이다**: `add()`, `subtract()` 등 모든 연산이 새 객체를 반환한다. 원본이 바뀌는 사고가 원천 차단된다
88+
- **용도별 타입 분리**: `PlainDate`(날짜만), `PlainTime`(시간만), `ZonedDateTime`(타임존 포함) 등 필요한 것만 쓰면 된다
89+
- **브라우저 지원**: Chrome 144+, Firefox 139+에서 사용 가능. Safari는 아직이라 프로덕션에서는 `@js-temporal/polyfill` 폴리필을 함께 쓰는 게 안전하다

0 commit comments

Comments
 (0)