New ideas for time formatting in JavaScript toLocaleString()

New ideas for time formatting in JavaScript toLocaleString()

When studying Object object, I saw toLocaleString() method, which can easily implement time formatting.

1. Conventional ideas for time formatting

The normal idea is to get the year, month, day, etc. in sequence through the Date instance, such as 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)
        .replace("月", month)
        .replace("日", day)
        .replace("hour", hour)
        .replace("minute", minute)
        .replace("秒", second);
    return dateStr;
};
 
// Using the above method console.log(new Date().format("year-month-day")); // 2021-11-04

2. Time formatting toLocaleString()

toLocaleString() is similar to toString() in that it also returns a string of the object, but it is processed according to the localized execution environment. In particular, it supports time objects and can be converted into 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 the local time format console.log(date.toLocaleString()); // 2021/11/4 10:18:08 AM


New browser versions can support locales and options parameters:

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


Get the day of the week:

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

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

defect:

If you want to display the format of x year x month x day, there is currently no suitable way to write it. Relatively speaking, the toLocaleString() function is more limited.

This concludes this article about the new idea of ​​time formatting in JavaScript toLocaleString() For more relevant JavaScript toLocaleString() content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • isPrototypeOf Function in JavaScript
  • Detailed explanation of JavaScript prototype chain
  • JavaScript Composition and Inheritance Explained
  • Detailed explanation of js event delegation
  • nuxt.js multiple environment variable configuration
  • Differences and usage examples of for, for...in, for...of and forEach in JS
  • Javascript uses the integrity attribute for security verification

<<:  The difference between html empty link href="#" and href="javascript:void(0)"

>>:  Analysis of three parameters of MySQL replication problem

Recommend

MySQL 5.7.30 Installation and Upgrade Issues Detailed Tutorial

wedge Because the MySQL version installed on the ...

How to set underline in HTML? How to underline text in HTML

Underlining in HTML used to be a matter of enclos...

Detailed explanation of the use of MySQL mysqldump

1. Introduction to mysqldump mysqldump is a logic...

MySQL Series 13 MySQL Replication

Table of contents 1. MySQL replication related co...

WeChat applet implements a simple calculator

WeChat applet's simple calculator is for your...

CSS Standard: vertical-align property

<br />Original text: http://www.mikkolee.com...

Ubuntu 18.04 disable/enable touchpad via command

In Ubuntu, you often encounter the situation wher...

How to configure path alias for react scaffolding

The react version when writing this article is 16...

MySQL View Principle Analysis

Table of contents Updatable Views Performance of ...

js basic syntax and maven project configuration tutorial case

Table of contents 1. js statement Second, js arra...

Methods for defragmenting and reclaiming space in MySQL tables

Table of contents Causes of MySQL Table Fragmenta...