簡體   English   中英

如何使用momentjs從日期中獲取日期名稱

[英]How to get name of the day from date with momentjs

我正在使用momentjs並想輸出當天的名稱,即。 “星期三”,但 API 似乎只提供了一個數字。

有沒有辦法在不將其硬編碼為特定語言的情況下做到這一點?

他們文檔格式部分

星期dddd星期日 星期一 ... 星期五 星期六

moment().format('dddd');

以防萬一,如果您想從索引日中獲取名稱

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

使用moment().format('dddd'); 獲得一天的全名,如 'Sunday' , 'Monday'

使用moment().format('ddd'); 得到三個字母的名字,比如 '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