Get the App
SLTechnology News&Howtos  ›  Development  › 

Analyze the new idea of time formatting in JavaScript toLocaleString ()

Shulou Source: shulou.com Published: 2022-06-02 17:58:37 10月03日 Update

This article focuses on "analyzing the new idea of time formatting in JavaScript toLocaleString ()". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "analyze the new idea of time formatting in JavaScript toLocaleString ()"!

1. The general idea of time formatting

The normal idea is to obtain the year, month, day, etc., in turn through the instance of Date, for example, a simple formatting example:

Date.prototype.format = function (dateStr) {let date = new Date (); let year = date.getFullYear (); let month = date.getMonth () + 1; let day = date.getDate (). ToString (). PadStart (2, "0"); let hour = date.getHours (); let minute = date.getMinutes (); let second = date.getSeconds () DateStr = dateStr.replace ("year", year). Replace ("month", month). Replace ("day", day). Replace ("hour", hour). Replace ("minute", minute). Replace ("second", second); return dateStr;}; / / use the above method console.log (new Date (). Format ("year-month-day")) / / 2021-11-042, time formatted toLocaleString ()

ToLocaleString (), like toString (), is a string that returns an object, but is handled according to the localized execution environment. In particular, the support for time objects can be converted to a certain format.

/ date, output current time let date = new Date (); / / this is Greenwich mean time format console.log (date.toString ()); / / Thu Nov 04 2021 10:11:35 GMT+0800 (China Standard time) / / this is local time format console.log (date.toLocaleString ()); / / 10:18:08 on 2021-11-4

The new version of the browser can support locales and options parameters:

Let date = new Date (); / / 24-hour let options = {year: 'numeric', month:' numeric', day: 'numeric', hour:' numeric', minute: 'numeric', second:' numeric', hour12: false}; console.log (date.toLocaleString ("zh-CN", options)); / / 10:33:01 on 2021-11-4

Get the day of the week:

Let date = new Date (); let options = {weekday: "long"}; console.log (date.toLocaleString ("zh-CN", options)); / / Thursday

For more parameters of options, please refer to the link provided at the end of the article.

Defect:

If you want to display a format like x year, month and day, there is currently no suitable way to write it, and the function of toLocaleString () is relatively limited.

At this point, I believe you have a deeper understanding of "analyzing the new idea of time formatting in JavaScript toLocaleString ()". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Format time idea middle time analysis content parameter object hour method week more learning support appropriate practical deeper example interest writing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info NVidia MariaDB vpn Redmi