【发布时间】:2026-01-13 01:35:02
【问题描述】:
我正在使用momentjs 并想输出当天的名称,即。 “星期三”,但 API 似乎只提供了一个数字。
有没有办法在不将其硬编码为特定语言的情况下做到这一点?
【问题讨论】:
标签: javascript momentjs
我正在使用momentjs 并想输出当天的名称,即。 “星期三”,但 API 似乎只提供了一个数字。
有没有办法在不将其硬编码为特定语言的情况下做到这一点?
【问题讨论】:
标签: javascript momentjs
【讨论】:
使用 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>
【讨论】:
如果您想从索引日中获取名称
const day = 1; //second index after 0
moment().day(day).format("dddd") //Monday
【讨论】: