Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

javascript - 如何格式化JavaScript日期(How to format a JavaScript date)

如何格式化JavaScript日期对象以将其打印为10-Aug-2010

  ask by leora translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use toLocaleDateString()(使用toLocaleDateString())

The toLocaleDateString() method returns a string with a language-sensitive representation of the date portion of the date.

(toLocaleDateString()方法返回一个字符串,其中包含日期的日期部分的语言敏感表示。)

The locales and options arguments let applications specify the language whose formatting conventions should be used and allow to customize the behavior of the function.

(语言环境和选项参数使应用程序可以指定应使用其格式约定的语言,并允许自定义函数的行为。)

The values you can passed in options for different keys:(您可以在不同键的选项中传递的值:)

  1. day:

    (天:)


    The representation of the day.

    (一天的表示形式。)


    Possible values are "numeric", "2-digit".

    (可能的值为“数字”,“ 2位”。)

  2. weekday:

    (工作日:)


    The representation of the weekday.

    (工作日的表示形式。)


    Possible values are "narrow", "short", "long".

    (可能的值为“ narrow”,“ short”,“ long”。)

  3. year:

    (年:)


    The representation of the year.

    (年份的表示形式。)


    Possible values are "numeric", "2-digit".

    (可能的值为“数字”,“ 2位”。)

  4. month:

    (月:)


    The representation of the month.

    (月份的表示形式。)


    Possible values are "numeric", "2-digit", "narrow", "short", "long".

    (可能的值为“数字”,“两位数”,“窄”,“短”,“长”。)

  5. hour:

    (小时:)


    The representation of the hour.

    (小时的表示形式。)


    Possible values are "numeric", "2-digit".

    (可能的值为“数字”,“ 2位”。)

  6. minute: The representation of the minute.

    (分钟:分钟的表示形式。)


    Possible values are "numeric", "2-digit".

    (可能的值为“数字”,“ 2位”。)

  7. second:

    (第二:)


    The representation of the second.

    (第二个的表示形式。)


    Possible values are "numeric", 2-digit".

    (可能的值为“数字”,“ 2位数字”。)

All these keys are optional.

(所有这些键都是可选的。)

You can change the number of options values based on your requirements, and this will also reflect the presence of each date time term.

(您可以根据需要更改选项值的数量,这也将反映每个日期时间项的存在。)

Note: If you would only like to configure the content options, but still use the current locale, passing null for the first parameter will cause an error.

(注意:如果只想配置内容选项,但仍使用当前语言环境,则为第一个参数传递null会导致错误。)

Use undefined instead.

(请改用undefined 。)

For different languages:(对于不同的语言:)

  1. "en-US": For English

    (“ en-US”:英语)

  2. "hi-IN": For Hindi

    (“ hi-IN”:用于印地语)

  3. "ja-JP": For Japanese

    (“ ja-JP”:对于日语)

You can use more language options.

(您可以使用更多语言选项。)

For example(例如)

 var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; var today = new Date(); console.log(today.toLocaleDateString("en-US")); // 9/17/2016 console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016 console.log(today.toLocaleDateString("hi-IN", options)); // ??????, 17 ?????? 2016 

You can also use the toLocaleString() method for the same purpose.

(您也可以出于相同目的使用toLocaleString()方法。)

The only difference is this function provides the time when you don't pass any options.

(唯一的区别是此功能提供了您不传递任何选项的时间。)

// Example
9/17/2016, 1:21:34 PM

References:(参考文献:)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...