js date and time formatting method example

js date and time formatting method example

js date time format

Convert 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 time

Moment.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:

Format Codes illustrate Return value example
M Month as a number, without a leading zero 1 to 12
MM Month as a number, with a leading zero 01 to 12
MMM Three-letter month abbreviation Jan to Dec
MMMM Month, full text format January to December
Q Quarter 1 to 4
D Day of the month, without leading zero 1 to 31
DD Day of the month, with leading zero 01 to 31
d Day of the week, represented by a number 0 to 6, 0 represents Sunday and 6 represents Saturday
ddd Three letters representing the day of the week Sun to Sat
dddd Day of the week, full day of the week text From Sunday to Saturday
w Week of year Such as 42: means the 42nd week
YYYY Four-digit year For example: 2014 or 2000
YY Two-digit year For example: 14 or 98
A AM PM in capital letters AM PM
a Lowercase am pm am pm
HH Hour, 24-hour format, with leading zero 00 to 23
H Hour, 24-hour format, no leading zero 0 to 23
hh Hour, 12-hour format, with leading zero 00 to 12
h Hour, 12-hour format, no leading zero 0 to 12
m Minutes without leading zeros 0 to 59
mm Minutes with leading zeros 00 to 59
s Seconds without leading zeros 1 to 59
ss Description with leading zeros 01 to 59
X Unix timestamp 1411572969

For more information about moment.js, please visit the project's official website: http://momentjs.com/

Summarize

This 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:
  • JS method to get the current time of year, month, day, hour, minute, second and time format
  • Several methods of time formatting in js
  • js formats the current time into year-month-day hour: minute: second implementation code
  • Introduction to various methods of formatting js timestamp into date format
  • A simple example of js formatting time
  • Js gets the current date and time and format code
  • js method to format time
  • js time and date formatting encapsulation function
  • Summary of js formatting time
  • Summary of js formatting time and date functions
  • Five ways to format time in JS

<<:  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"

Recommend

Tutorial on disabling and enabling triggers in MySQL [Recommended]

When using MYSQL, triggers are often used, but so...

Summary of the differences and usage of plugins and components in Vue

The operating environment of this tutorial: Windo...

404 error occurs when accessing the homepage of tomcat started in Docker mode

Scenario: When starting tomcat in docker (version...

MySQL Database Indexes and Transactions

Table of contents 1. Index 1.1 Concept 1.2 Functi...

MySQL 5.7.13 installation and configuration method graphic tutorial on Mac

MySQL 5.7.13 installation tutorial for Mac, very ...

Common functions of MySQL basics

Table of contents 1. Common function classificati...

Overview and Introduction to Linux Operating System

Table of contents 1. What is an Operating System ...

Tips for creating two-dimensional arrays in JavaScript

Creation of a two-dimensional array in Js: First ...

MySQL database terminal - common operation command codes

Table of contents 1. Add users 2. Change the user...

Detailed explanation of the use of React.cloneElement

Table of contents The role of cloneElement Usage ...

Web Design Tutorial (8): Web Page Hierarchy and Space Design

<br />Previous article: Web Design Tutorial ...

How to prevent duplicate submission in jquery project

In new projects, axios can prevent duplicate subm...