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

18
我正在使用 momentjs,想要输出星期几的名称,例如"星期三",但API似乎只提供数字。
是否有一种方法可以不硬编码到特定语言而实现此目的?
3个回答

43

从他们的文档中格式部分

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

(Note: I have translated the original text into simplified Chinese.)
moment().format('dddd');

3
如果你想从索引中获取一个名称
const day = 1; //second index after 0
moment().day(day).format("dddd") //Monday

1
使用moment().format('dddd');获取全名日期,例如“星期日”,“星期一”。 使用moment().format('ddd');获取三个字母的日期名称,例如“日”,“一”。

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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接