我正在使用 momentjs,我想返回过去 7 天的所有名称。我知道我可以使用以下方法获取号码:
moment().isoWeekday();
如何获得从今天起过去 7 天的姓名列表?例如:
tuesday, monday, sunday, saturday, friday, thursday, wednesday
这样的事情应该工作:
let resultDates = []; // array to hold day names
const current = moment(); // current date
let n = 7; // days to go back
while (n > 0) {
resultDates.push(current.format("dddd")) // get day n and push it to array
current.subtract(1, "day") // subtract a day
n--;
}
console.log(resultDates);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
数组的第一个元素是今天,最后一个元素是 7 天前。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句