Open In App

How to Format Datetime to YYYY-MM-DD HH:MM:SS in Moment.js?

Last Updated : 01 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Moment.js, formatting the date in the proper format is important for consistent data presentation and manipulation. The library provides various methods to handle and transform dates. You can use direct formatting with format(), manipulate ISO strings, or work with Unix timestamps to achieve the desired 'YYYY-MM-DD HH:mm:ss' format.

Run the below command before running the code in your local system:

npm i moment

1. Using format Method Directly

In this approach, we are using the format method directly to convert the input date string from 'MM/DD/YYYY HH:mm:ss' format to 'YYYY-MM-DD HH:mm:ss'. By parsing the input date with the specified format and then formatting it using format(), we achieve the desired output format.

Example: The below example uses the format Method Directly to Format datetime to YYYY-MM-DD HH:mm:ss in moment.js.

JavaScript
// script.js

const moment = require('moment');
const date = moment('07/24/2024 08:00:00', 'MM/DD/YYYY HH:mm:ss');
const res = date.format('YYYY-MM-DD HH:mm:ss');
console.log(`Formatted date: ${res}`);

Note: Use command, node script.js to see console.

Output

Formatted date: 2024-07-24 08:00:00

2. Using toISOString and String Manipulation

In this approach, we are using toISOString to convert the Moment.js object to a standardized ISO string. We then extract the relevant portion of the ISO string (up to the first 19 characters) and replace the 'T' separator with a space to format it as 'YYYY-MM-DD HH:mm:ss'.

Example: The below example uses the format Method Directly to Format datetime to YYYY-MM-DD HH:mm:ss in moment.js.

JavaScript
// script.js

const moment = require('moment');
const date = moment('2024-07-24T08:00:00Z', 'YYYY-MM-DDTHH:mm:ssZ');
const isoString = date.toISOString();
const res = isoString.substring(0, 19).replace('T', ' ');
console.log(`Formatted date: ${res}`);

Note: Use command, node script.js to see console.

Output

Formatted date: 2024-07-24 08:00:00

3. Using unix Timestamp with Manual Formatting

In this approach, we are using a Unix timestamp to handle date formatting. We first convert the input date string into a Unix timestamp, then use moment.unix() to create a new Moment.js object from this timestamp. Finally, we format this object to 'YYYY-MM-DD HH:mm:ss', resulting in the desired date format.

Example: The below example uses the format Method Directly to Format datetime to YYYY-MM-DD HH:mm:ss in moment.js.

JavaScript
// script.js

const moment = require('moment');
const date = moment('Wed Jul 24 2024 08:00:00', 'ddd MMM DD YYYY HH:mm:ss');
const unixTimestamp = date.unix();
const res = moment.unix(unixTimestamp).format('YYYY-MM-DD HH:mm:ss'); 
console.log(`Formatted date: ${res}`);

Note: Use command, node script.js to see console.

Output:

Formatted date: 2024-07-24 08:00:00

Next Article

Similar Reads