百科狗-知识改变命运!
--

Date.prototype.toLocaleDateString() - JavaScript Date 对象

百变鹏仔1年前 (2023-11-21)阅读数 18#技术干货
文章标签参数

Date.prototype.toLocaleDateString()

toLocaleDateString()方法返回该日期对象日期部分的字符串,该字符串格式因不同语言而不同。新增的参数localesoptions使程序能够指定使用哪种语言格式化规则,允许定制该方法的表现(behavior)。在旧版本浏览器中,localesoptions参数被忽略,使用的语言环境和返回的字符串格式是各自独立实现的。

语法

dateObj.toLocaleDateString([locales [, options]])

参数

查看浏览器兼容性小节,看下哪些浏览器支持localesoptions参数,还可以参看例子:检测localesoptions参数支持情况。

locales Optional

A string with a BCP 47 language tag, or an array of such strings. To use the browser's default locale, omit this argument or pass undefined. Unicode extension are supported(for example "en-US-u-ca-buddhist"). For the general form and interpretation of the locales argument, see the Intl page. The following Unicode extension keys are allowed:

nuNumbering system. Possible values include:" arab"," arabext"," bali"," beng"," deva"," fullwide"," gujr"," guru"," hanidec"," khmr"," knda"," laoo"," latn"," limb"," mlym"," mong"," mymr"," orya"," tamldec"," telu"," thai"," tibt".caCalendar. Possible values include:" buddhist"," chinese"," coptic"," ethiopia"," ethiopic"," gregory"," hebrew"," indian"," islamic"," iso8601"," japanese"," persian"," roc".hcHour cycle. Possible values include:" h11"," h12"," h23"," h24".options Optional

An object with some or all of the following properties:

dateStyleThe date formatting style to use when calling format(). Possible values include:
  • "full"
  • "long"
  • "medium"
  • "short"
timeStyleThe time formatting style to use when calling format(). Possible values include:
  • "full"
  • "long"
  • "medium"
  • "short"
localeMatcherThe locale matching algorithm to use. Possible values are "lookup" and "best fit"; the default is "best fit". For information about this option, see the Intl page.timeZoneThe time zone to use. The only value implementations must recognize is "UTC"; the default is the runtime's default time zone. Implementations may also recognize the time zone names of the IANA time zone database, such as "Asia/Shanghai"," Asia/Kolkata"," America/New_York".hour12Whether to use 12-hour time(as opposed to 24-hour time). Possible values are true and false; the default is locale dependent. This option overrides the hc language tag and/or the hourCycle option in case both are present.hourCycleThe hour cycle to use. Possible values are "h11"," h12"," h23", or "h24". This option overrides the hc language tag, if both are present, and the hour12 option takes precedence in case both options have been specified.formatMatcherThe format matching algorithm to use. Possible values are "basic" and "best fit"; the default is "best fit". See the following paragraphs for information about the use of this property.

The following properties describe the date-time components to use in formatted output, and their desired representations. Implementations are required to support at least the following subsets:

  • weekday,year,month,day,hour,minute,second
  • weekday,year,month,day
  • year,month,day
  • year,month
  • month,day
  • hour,minute,second
  • hour,minute

Implementations may support other subsets, and requests will be negotiated against all available subset-representation combinations to find the best match. Two algorithms are available for this negotiation and selected by the formatMatcher property: A fully specified "basic" algorithm and an implementation-dependent "best fit" algorithm.

weekdayThe representation of the weekday. Possible values are:
  • "long"(e.g.,Thursday)
  • "short"(e.g.,Thu)
  • "narrow"(e.g.,T). Two weekdays may have the same narrow style for some locales(e.g.Tuesday's narrow style is also T).
eraThe representation of the era. Possible values are:
  • "long"(e.g.,Anno Domini)
  • "short"(e.g.,AD)
  • "narrow"(e.g.,A)
yearThe representation of the year. Possible values are:
  • "numeric"(e.g.,2012)
  • "2-digit"(e.g.,12)
monthThe representation of the month. Possible values are:
  • "numeric"(e.g.,2)
  • "2-digit"(e.g.,02)
  • "long"(e.g.,March)
  • "short"(e.g.,Mar)
  • "narrow"(e.g.,M). Two months may have the same narrow style for some locales(e.g.May's narrow style is also M).
dayThe representation of the day. Possible values are:
  • "numeric"(e.g.,1)
  • "2-digit"(e.g.,01)
hourThe representation of the hour. Possible values are "numeric"," 2-digit".minuteThe representation of the minute. Possible values are "numeric"," 2-digit".secondThe representation of the second. Possible values are "numeric"," 2-digit".timeZoneNameThe representation of the time zone name. Possible values are:
  • "long"(e.g.,British Summer Time)
  • "short"(e.g.,GMT+1)

The default value for each date-time component property is undefined, but if the weekday,year,month,day properties are all undefined, then year,month, and day are assumed to be "numeric".

例子

例子:使用toLocaleDateString

没有指定语言环境(locale)时,返回一个使用默认语言环境和格式设置(options)的格式化字符串。

var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));

// toLocaleDateString without arguments depends on the implementation,
// the default locale, and the default time zone
date.toLocaleDateString();
// → "12/11/2012" if run in en-US locale with time zone America/Los_Angeles

例子:检测localesoptions参数支持情况

localesoptions参数不是所有的浏览器都支持。为了检测一种实现环境(implementation)是否支持它们,可以使用不合法的语言标签,如果实现环境支持该参数,则会抛出一个RangeError异常,反之会忽略参数。

function toLocaleDateStringSupportsLocales() {
    try {
        new Date().toLocaleDateString("i");
    } catch (e) {
        return e.name === "RangeError";
    }
    return false;
}

例子:使用locales

下例展示了本地化日期格式的一些变化。为了在应用的用户界面得到某种语言的日期格式,必须确保使用locales参数指定了该语言(可能还需要设置某些回退语言)。

var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

// formats below assume the local time zone of the locale;
// America/Los_Angeles for the US

// US English uses month-day-year order
alert(date.toLocaleDateString("en-US"));
// → "12/19/2012"

// British English uses day-month-year order
alert(date.toLocaleDateString("en-GB"));
// → "20/12/2012"

// Korean uses year-month-day order
alert(date.toLocaleDateString("ko-KR"));
// → "2012. 12. 20."

// Arabic in most Arabic speaking countries uses real Arabic digits
alert(date.toLocaleDateString("ar-EG"));
// → "٢٠‏/١٢‏/٢٠١٢"

// for Japanese, applications may want to use the Japanese calendar,
// where 2012 was the year 24 of the Heisei era
alert(date.toLocaleDateString("ja-JP-u-ca-japanese"));
// → "24/12/20"

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
alert(date.toLocaleDateString(["ban", "id"]));
// → "20/12/2012"

例子:使用options

可以使用options 参数来自定义toLocaleDateString方法返回的字符串。

var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

// request a weekday along with a long date
var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
alert(date.toLocaleDateString("de-DE", options));
// → "Donnerstag, 20. Dezember 2012"

// an application may want to use UTC and make that visible
options.timeZone = "UTC";
options.timeZoneName = "short";
alert(date.toLocaleDateString("en-US", options));
// → "Thursday, December 20, 2012, GMT"

性能

Date.prototype.toLocaleDateString() - JavaScript Date 对象

当格式化大量日期时,最好创建一个Intl.DateTimeFormat对象,然后使用该对象format属性提供的方法。

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)