0Pricing
JavaScript Academy · 课时

日期——基础、时区与 Intl.DateTimeFormat

创建日期、读取各部分、按地区格式化,并使用 Intl.DateTimeFormat 输出其他时区的时间

日期——基础、时区与 Intl.DateTimeFormat 是 CoddyKit 上的免费 JavaScript Academy 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 JavaScript Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 JavaScript Academy 课程共包含 3 节课。

日期简介

目标:安全地创建和格式化日期。

  • new Date()
  • 读取各个部分
  • 本地化格式化
  • 其他时区
日期——基础、时区与 Intl.DateTimeFormat — 插图 1

创建日期值

可以使用 new Date()、时间戳或简单的年/月/日部分创建日期(月份索引从 0 开始)。

// Create Date instances
// Now (current time)
const now = new Date();

// From timestamp (milliseconds since Jan 1, 1970 UTC)
const fromMs = new Date(0);

// From parts: year, monthIndex (0=Jan), day
const xmas = new Date(2025, 11, 25);

console.log("Now:", now.toString());
console.log("Epoch:", fromMs.toUTCString());
console.log("Xmas:", xmas.toDateString());
日期——基础、时区与 Intl.DateTimeFormat — 插图 2

获取日期部分

使用 getFullYear、getMonth、getDate、getHours 等取值器。

// Read parts with getters
const d = new Date(2025, 0, 2, 15, 30);

console.log("Year:", d.getFullYear());
console.log("Month (0=Jan):", d.getMonth());
console.log("Day of month:", d.getDate());
console.log("Hours:", d.getHours());
console.log("Minutes:", d.getMinutes());
日期——基础、时区与 Intl.DateTimeFormat — 插图 3

ISO 与纪元时间

toISOString 会提供稳定的 UTC 字符串。getTime 会返回自纪元以来的毫秒数。

// ISO string and timestamps
const sample = new Date();

console.log("ISO:", sample.toISOString());
console.log("Unix seconds (approx):", Math.floor(sample.getTime() / 1000));
日期——基础、时区与 Intl.DateTimeFormat — 插图 4

本地化格式化

使用 Intl.DateTimeFormat,根据本地化设置清晰地输出日期,并通过选项指定日期部分。

// Format for a locale
const date = new Date(2025, 6, 4, 9, 5);

// Simple: toLocaleString uses your environment defaults
console.log("Local:", date.toLocaleString());

// Intl.DateTimeFormat with explicit locale and options
const fmt = new Intl.DateTimeFormat("en-GB", {
  year: "numeric",
  month: "short",
  day: "2-digit",
  hour: "2-digit",
  minute: "2-digit"
});

console.log("Formatted:", fmt.format(date));
日期——基础、时区与 Intl.DateTimeFormat — 插图 5

格式化其他时区

选择一个时区,即可查看同一时刻在不同地区的表示方式(无需手动计算)。

// Format for a different time zone
const meeting = new Date(Date.UTC(2025, 3, 10, 12, 0));

const ny = new Intl.DateTimeFormat("en-US", {
  timeZone: "America/New_York",
  dateStyle: "medium",
  timeStyle: "short"
});

const tokyo = new Intl.DateTimeFormat("en-US", {
  timeZone: "Asia/Tokyo",
  dateStyle: "medium",
  timeStyle: "short"
});

console.log("NY:", ny.format(meeting));
console.log("Tokyo:", tokyo.format(meeting));
日期——基础、时区与 Intl.DateTimeFormat — 插图 6

Intl.DateTimeFormat 小测

快速检查:本地化和时区格式化。

日期——基础、时区与 Intl.DateTimeFormat — 插图 7

回顾

回顾:您创建了日期,读取了日期部分,输出了 ISO,根据本地化设置进行了格式化,并使用 Intl.DateTimeFormat 显示了其他时区。

日期——基础、时区与 Intl.DateTimeFormat — 插图 8

常见问题解答

「日期——基础、时区与 Intl.DateTimeFormat」课时是免费的吗?

是的 — 「日期——基础、时区与 Intl.DateTimeFormat」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 JavaScript Academy 课程的其余内容,请升级到 CoddyKit PRO。 JavaScript Academy 课程共包含 3 节课。

「日期——基础、时区与 Intl.DateTimeFormat」这节课中我会学到什么?

创建日期、读取各部分、按地区格式化,并使用 Intl.DateTimeFormat 输出其他时区的时间 你通过在浏览器中直接运行的动手代码来练习 JavaScript Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 JavaScript Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 JavaScript Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。

「日期——基础、时区与 Intl.DateTimeFormat」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 JavaScript Academy 课中编写并运行代码吗?

能。每节 JavaScript Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 模板字面量与常用字符串操作
  2. 数字——整数与浮点数及 Math 基础
  3. 日期——基础、时区与 Intl.DateTimeFormat
← 返回 JavaScript Academy