How to modify date format using moment.js?

Member

by dana , in category: Javascript , a year ago

How to modify date format using moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by tressie.damore , a year ago

@dana 

To modify the date format using moment.js, you can use the format method. Here's an example of how to change the date format from the default format to a custom format:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Include moment.js library
const moment = require('moment');

// Create a date object
let date = moment();

// Format the date using the 'YYYY-MM-DD' format
let formattedDate = date.format('YYYY-MM-DD');

console.log(formattedDate);


In the example above, we first include the moment.js library and create a new moment instance. We then use the format method to change the date format to the custom format 'YYYY-MM-DD'. You can change the format string to any desired format according to your requirements.

Related Threads:

How to add and format date in moment.js?
How to convert new date() to utc date using moment.js?
How to convert date to gmt format using oracle?
How to get time format to hh:mm using moment.js?
How to add days using moment.js with format of mm/dd/yyyy?
How to change the date using moment.js?