js date time formatConvert the date and time to the specified format, such as: YYYY-mm-dd HH:MM represents 2019-06-06 19:45 function dateFormat(fmt, date) { let ret; const opt = { "Y+": date.getFullYear().toString(), // year "m+": (date.getMonth() + 1).toString(), // month "d+": date.getDate().toString(), // day "H+": date.getHours().toString(), // hour "M+": date.getMinutes().toString(), // minute "S+": date.getSeconds().toString() // second // You can continue to add other formatting characters if you need them, and they must be converted to strings}; for (let k in opt) { ret = new RegExp("(" + k + ")").exec(fmt); if (ret) { fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0"))) }; }; return fmt; } usage: let date = new Date() dateFormat("YYYY-mm-dd HH:MM", date) >>> 2019-06-06 19:45` If you have a lot of requirements for date and time processing, I recommend moment.js, a date processing library that is simple and convenient. Moment.js format date timeMoment.js is a lightweight JavaScript time library that facilitates time operations in daily development and improves development efficiency. In daily development, we usually perform the following operations on time: such as getting time, setting time, formatting time, comparing time, etc. Formatting Dates Current time: moment().format('YYYY-MM-DD HH:mm:ss'); //2014-09-24 23:36:09 What day is today? moment().format('d'); //3 Convert the current time to Unix timestamp: moment().format('X'); Relative time 20120901 is 2 years ago relative to the current date moment("20120901", "YYYYMMDD").fromNow(); //2 years ago Date after 7 days: moment().add('days',7).format('YYYY year MM month DD day'); //October 1, 2014 Time after 9 hours: moment().add('hours',9).format('HH:mm:ss'); moment.js provides rich documentation, and can also be used to create complex date and time applications such as calendar projects. The most commonly used format in our daily development is time formatting. Below I have made a table of commonly used formats for friends in need to view:
For more information about moment.js, please visit the project's official website: http://momentjs.com/ SummarizeThis is the end of this article about js date and time formatting. For more relevant js date and time formatting content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Solution to the problem of Windows Server 2008 r2 server automatically restarting for no reason
>>: Solution to the MySQL error "Every derived table must have its own alias"
1. Tools We need two tools now: MySQL server (mys...
1. System environment The system version after yu...
Public name of the page: #wrapper - - The outer e...
When the carriage return character ( Ctrl+M ) mak...
1. scroll-view When using vertical scrolling, you...
Preface In some cases, we only know the intranet ...
The benefits of using MySQL master-slave replicat...
This article shares the specific code of jQuery t...
There are two ways to expose container ports in d...
1 Question The company's server uses Apache, ...
This CSS reset is modified based on Eric Meyers...
Problem description: Error message: Caused by: co...
Today I will talk to you about clearing floats. B...
Table of contents Preface Axios installation and ...
Preface Sometimes, we need a floating effect requ...