Skip to content

Instantly share code, notes, and snippets.

@mmyoji
Last active May 8, 2026 01:15
Show Gist options
  • Select an option

  • Save mmyoji/7d25304dc8625aad92d5e86a4e250bdc to your computer and use it in GitHub Desktop.

Select an option

Save mmyoji/7d25304dc8625aad92d5e86a4e250bdc to your computer and use it in GitHub Desktop.
Migration from Day.js to Temporal API
/**
* date string
*/
const s = "20260430";
const d1 = Temporal.PlainDate.from(s); // 2026-04-30
const d2 = Temporal.Now.plainDateISO(); // 2026-05-01
// .isBefore() / .isAfter()
Temporal.PlainDate.compare(d1, d2); // -1
Temporal.PlainDate.compare(d1, d1); // 0
Temporal.PlainDate.compare(d2, d1); // 1
/**
* .startOf() / .endOf()
*/
const t = Temporal.Now.zonedDateTimeISO();
// start of Day
t.startOfDay();
// start of Month
t.with({ day: 1 }).startOfDay();
// start of Year
t.with({ day: 1, month: 1 }).startOfDay();
// end of Day
t.add({ days: 1 }).startOfDay().subtract({ nanoseconds: 1 });
// end of Month
t.add({ months: 1 }).with({ day: 1 }).startOfDay().subtract({ nanoseconds: 1 });
// end of Year
t.add({ years: 1 }).with({ day: 1, month: 1 }).startOfDay().subtract({ nanoseconds: 1 });
/**
* TimeZone
*/
t.withTimeZone("Asia/Tokyo");
/**
* Type Conversions
*/
// Instant -> Date
new Date(Temporal.Now.instant().epochMilliseconds);
// ZonedDateTime -> Date
new Date(Temporal.Now.zonedDateTimeISO().epochMilliseconds);
// Date -> Instant
new Date().toTemporalInstant();
// Date -> ZonedDateTime
new Date().toTemporalInstant().toZonedDateTimeISO(Temporal.Now.timeZoneId());
/**
* Format
* - Intl.DateTimeFormat doesn't care about TimeZone
*/
// "YYYY-MM-DD"
Intl.DateTimeFormat("en-CA", { dateStyle: "short" }).format(dateLike);
// "YYYY-MM-DD HH:mm"
Intl.DateTimeFormat("en-CA", {
year: "numeric", month: "2-digit", day: "2-digit",
hour: "2-digit", minute: "2-digit", hour12: false,
}).format(dateLike).replace(",", "");
// "YYYY/MM/DD"
Intl.DateTimeFormat("ja-JP", { dateStyle: "medium" }).format(dateLike);
Intl.DateTimeFormat("ja-JP", { dateStyle: "short" }).format(dateLike);
// "YYYY/MM/DD HH:mm"
Intl.DateTimeFormat("ja-JP", { dateStyle: "medium", timeStyle: "short" }).format(dateLike);
Intl.DateTimeFormat("ja-JP", { dateStyle: "short", timeStyle: "short" }).format(dateLike);
// "MM/DD"
Intl.DateTimeFormat("ja-JP", { month: "2-digit", day: "2-digit" }).format(dateLike);
// "HH:mm:ss"
Intl.DateTimeFormat("ja-JP", { timeStyle: "medium" }).format(dateLike);
// "HH:mm"
Intl.DateTimeFormat("ja-JP", { timeStyle: "short" }).format(dateLike);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment