Getting the month number by month name in Moment.js

To get the number of the month using Moment.js

jsx
import moment from 'moment';

let numberOfMonth = moment().month('October').format('M');  // Number between 1 and 12
let numberOfMonth = moment().month('October').format('MM'); // Number between 01 and 12

To get the full date:

jsx
import moment from 'moment';

let fullDate = moment().year(2025).month('October').date(27).format('YYYYMMDD');  // 20251027

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.