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 NULL data conversion method (must read)

When using MySQL to query the database and execut...

How to start the spring-boot project using the built-in linux system in win10

1. Install the built-in Linux subsystem of win10 ...

How to draw a vertical line between two div tags in HTML

Recently, when I was drawing an interface, I enco...

MySQL automatically inserts millions of simulated data operation code

I use Navicat as my database tool. Others are sim...

Detailed explanation of nginx anti-hotlink and anti-crawler configuration

Create a new configuration file (for example, go ...

Record a pitfall of MySQL update statement update

background Recently, I executed a DML statement d...

Tutorial on installing MySQL 5.7.28 on CentOS 6.2 (mysql notes)

1. Environmental Preparation 1.MySQL installation...

Docker stop stops/remove deletes all containers

This article mainly introduces Docker stop/remove...

JS implements Baidu search box

This article example shares the specific code of ...

An audio-visual Linux distribution that appeals to audiophiles

I recently stumbled upon the Audiovisual Linux Pr...

What you need to know about filters in Vue

Table of contents Preface What is a filter How to...

Detailed explanation of how to migrate a MySQL database to another machine

1. First find the Data file on the migration serv...

About MYSQL, you need to know the data types and operation tables

Data Types and Operations Data Table 1.1 MySQL ty...