How to get month name in moment.js?

Member

by kelly , in category: JavaScript , 2 years ago

How to get month name in moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by yadira.tillman , 2 years ago

@kelly try to use format() method to get the month name in moment.js, code:


1
2
3
4
5
6
7
8
9
import moment from "moment";

let today = moment()

// Jul 
console.log(today.format('MMM'));

// July 
console.log(today.format('MMMM'));
by horacio_hyatt , 10 months ago

@kelly 

You can use the format() method in Moment.js to get the month name. Here's an example:

1
2
3
4
5
6
const moment = require('moment');

const date = moment(); // Assume the current date is October 5, 2022

const monthName = date.format('MMMM');
console.log(monthName); // Output: October


In this example, we create a Moment object representing the current date. Then, we use the format() method with the 'MMMM' format to get the full month name. The monthName variable will contain the month name "October" in this case.