Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get shortname of month in moment.js?

i have successfully find the full name of month but i want short name of month. Can anyone help me. I am using Moment.js i have successfully find the month but i want in short form
here is my code :

var date = '<?php echo date("Y-m-d") ?>';
var Month = moment(date, 'YYY-MM-DD').format('DD/MMMM')
console.log(Month);

it returns me 06/February, But i want 06/Feb

Thanks in advance :)

like image 447
kunal Avatar asked Feb 06 '17 06:02

kunal


People also ask

How do you get month name from month number using moment?

To get month name from two digit month number with JavaScript, we can use the moment. js' format method. We call format with 'MMMM' to get the full month name. As a result, formattedMonth is 'October' since we called month with 9 before calling format .

How do you add 1 month in moment?

Try moment('2020-03-30'). add(1, 'months') and then compare with moment('2020-03-31'). add(1, 'months') .

What is Moment () hour ()?

The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().


3 Answers

You are not using correct format string, use MMM instead of MMMM see Docs

console.log(moment().format('DD/MMM'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
like image 64
Satpal Avatar answered Oct 17 '22 06:10

Satpal


As an addendum to Satpal's answer, a simpler (kind of faster/safer approach) would be to go with the Localized formats because preferred formatting differs based on locale.

moment().format('ll') - Sep 4, 1986

Where as

moment().format('LL') - September 4, 1986

Optionally to include time:

moment().format('lll') - Sep 4, 1986 8:30 PM

Where as

moment().format('LLL') - September 4, 1986 8:30 PM
like image 6
Adedoyin Akande Avatar answered Oct 17 '22 05:10

Adedoyin Akande


use below month formats to get required output :

enter image description here

console.log(`Month Number : moment().format('M') ==> `,moment().format('M'))

console.log(`Month Number : moment().format('Mo') ==> `,moment().format('Mo'))

console.log(`Month Number : moment().format('MM') ==> `,moment().format('MM'))

console.log(`3 Letter Month Name : moment().format('MMM') ==> `,moment().format('MMM'))

console.log(`Full Month Name : moment().format('MMMM') ==> `,moment().format('MMMM'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
like image 6
Saurabh Mistry Avatar answered Oct 17 '22 07:10

Saurabh Mistry