【发布时间】:2019-02-13 12:47:09
【问题描述】:
我正在使用 momentjs,我想返回过去 7 天的所有名称。我知道我可以使用以下方法获取号码:
moment().isoWeekday();
如何获取从今天起过去 7 天的姓名列表?例如:
tuesday, monday, sunday, saturday, friday, thursday, wednesday
【问题讨论】:
标签: momentjs
我正在使用 momentjs,我想返回过去 7 天的所有名称。我知道我可以使用以下方法获取号码:
moment().isoWeekday();
如何获取从今天起过去 7 天的姓名列表?例如:
tuesday, monday, sunday, saturday, friday, thursday, wednesday
【问题讨论】:
标签: momentjs
这样的事情应该可以工作:
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 天前。
【讨论】: