You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Date.toLocaleString()方法的options参数支持哪些可选属性?

Date.toLocaleString() Options Parameter: All Key Properties

Great question! The options parameter for Date.toLocaleString() is super flexible—it lets you tweak every part of how your date and time are formatted. Below’s a breakdown of all the useful optional properties, grouped by their purpose:

These control how the date portion of your string is displayed:

  • weekday: Sets the format for the day of the week. Choose from:
    • "narrow": Shortest possible form (e.g., "S" for Sunday)
    • "short": Abbreviated form (e.g., "Sun")
    • "long": Full name (e.g., "Sunday")
  • year: Formats the year. Options are:
    • "numeric": Full year (e.g., 2024)
    • "2-digit": Two-digit year (e.g., 24)
  • month: Controls how the month is shown. Valid values:
    • "numeric": Numeric month (e.g., 6 for June)
    • "2-digit": Two-digit numeric month (e.g., 06)
    • "narrow": Shortest text form (e.g., "J" for June)
    • "short": Abbreviated text (e.g., "Jun")
    • "long": Full month name (e.g., "June")
  • day: Formats the day of the month. Options:
    • "numeric": Numeric day (e.g., 15)
    • "2-digit": Two-digit day (e.g., 15)

These adjust the time portion of your formatted string:

  • hour: Formats the hour. Choose "numeric" or "2-digit"
  • minute: Formats minutes. Choose "numeric" or "2-digit"
  • second: Formats seconds. Choose "numeric" or "2-digit"
  • hour12: A boolean that forces 12-hour (AM/PM) or 24-hour time. Set to true for 12-hour, false for 24-hour; defaults to the locale's standard.
  • timeZone: Specifies the time zone to use (e.g., "UTC", "America/Los_Angeles", "Asia/Tokyo"). If omitted, uses the user's local time zone.
  • timeZoneName: Shows the time zone name. Options:
    • "short": Abbreviated name (e.g., "CST")
    • "long": Full time zone name (e.g., "China Standard Time")

General Formatting Properties

These affect how the locale and format are matched:

  • localeMatcher: Determines how the locales parameter is matched to available locales. Options:
    • "lookup": Fast, simple matching
    • "best fit": More precise matching (default)
  • formatMatcher: Controls how the date/time format is selected. Options:
    • "basic": Uses a basic set of format patterns
    • "best fit": Picks the most appropriate format for the locale (default)

Example Usage

Here’s a quick example combining multiple properties to get a fully customized string:

const date = new Date(Date.UTC(2024, 5, 15, 14, 30, 45));
const customOptions = {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
  hour12: true,
  timeZone: 'Asia/Shanghai',
  timeZoneName: 'short'
};

console.log(date.toLocaleString('zh-CN', customOptions));
// Output: 星期六, 2024年6月15日 10:30:45 下午 CST

内容的提问来源于stack exchange,提问作者therefriedbean

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 13:42:46