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

Detailed explanation of Javascript string methods

Table of contents String length: length charAt() ...

Detailed explanation of writing multiple conditions of CSS: not

The :not pseudo-class selector can filter element...

How to set the memory size of Docker tomcat

When installing Tomcat in Docker, Tomcat may over...

Basic statements of MySQL data definition language DDL

MySQL DDL statements What is DDL, DML. DDL is dat...

How to elegantly implement the mobile login and registration module in vue3

Table of contents Preface Input box component lay...

How to notify users of crontab execution results by email

symptom I set a crontab task on a centos7 host, b...

How to use custom images in Html to display checkboxes

If you need to use an image to implement the use ...

Nginx local directory mapping implementation code example

Sometimes you need to access some static resource...

Teach you how to install mysql database on Mac

Download MySQL for Mac: https://downloads.mysql.c...

WeChat applet implements jigsaw puzzle game

This article shares the specific code for impleme...

Solution to the error in compiling LVGL emulator on Linux

Table of contents 1. Error phenomenon 2. Error An...

Docker beginners' first exploration of common commands practice records

Before officially using Docker, let's first f...