简体   繁体   English

如何使用momentjs从日期中获取日期名称

[英]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.我正在使用momentjs并想输出当天的名称,即。 "Wednesday" but the API only seems to offer a number. “星期三”,但 API 似乎只提供了一个数字。

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星期dddd星期日 星期一 ... 星期五 星期六

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');使用moment().format('dddd'); to get full name of day like 'Sunday' , 'Monday'获得一天的全名,如 'Sunday' , 'Monday'

use moment().format('ddd');使用moment().format('ddd'); to get three letter name like 'Sun' , 'Mon'得到三个字母的名字,比如 '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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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