How to modify date format using moment.js?

Member

by dana , in category: Javascript , 3 months ago

How to modify date format using moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by tressie.damore , 3 months 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.