简体   繁体   中英

How to get name of the day from date with momentjs

I am using momentjs and want to output the name of the day ie. "Wednesday" but the API only seems to offer a number.

Is there a way to do this without hard-coding it to a particular language?

From the Format section of their documentation :

Day of Week dddd Sunday Monday ... Friday Saturday

moment().format('dddd');

In case, if you want to get a name out of an index day

const day = 1; //second index after 0
moment().day(day).format("dddd") //Monday

use moment().format('dddd'); to get full name of day like 'Sunday' , 'Monday'

use moment().format('ddd'); to get three letter name like 'Sun' , 'Mon'

 let day_name_full = moment().format('dddd'); let day_name_three_letter = moment().format('ddd'); console.log('== To Get Day Name =='); console.log("using format('dddd') =>",day_name_full); console.log("using format('ddd') =>",day_name_three_letter); console.log('== To Get Month Name =='); let month_name_full = moment().format('MMMM'); let month_name_three_letter = moment().format('MMM'); console.log("using format('MMMM') =>",month_name_full); console.log("using format('MMM') =>",month_name_three_letter);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM