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

How to get name of the day from date with momentjs

本文关键字:取日期 获取 日期 何使用 momentjs      更新时间:2023-09-26

我正在使用momentjs,想要输出当天的名称,即。"星期三",但 API 似乎只提供了一个数字。

有没有办法在不将其硬编码为特定语言的情况下做到这一点?

从其文档的格式部分:

星期几dddd星期天星期一...周五 周六

moment().format('dddd');

以防万一,如果您想从索引日中获取名称

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

使用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>