When studying 1. Conventional ideas for time formattingThe 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() // 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
This concludes this article about the new idea of time formatting in You may also be interested in:
|
<<: The difference between html empty link href="#" and href="javascript:void(0)"
>>: Analysis of three parameters of MySQL replication problem
When using MySQL to query the database and execut...
1. Install the built-in Linux subsystem of win10 ...
Recently, when I was drawing an interface, I enco...
I use Navicat as my database tool. Others are sim...
Create a new configuration file (for example, go ...
background Recently, I executed a DML statement d...
1. Environmental Preparation 1.MySQL installation...
This article mainly introduces Docker stop/remove...
In the previous article, we have implemented loca...
This article example shares the specific code of ...
I recently stumbled upon the Audiovisual Linux Pr...
Custom Image FAQ How to view the data disk? You c...
Table of contents Preface What is a filter How to...
1. First find the Data file on the migration serv...
Data Types and Operations Data Table 1.1 MySQL ty...