我正在尝试创建一个函数来获取当天的所有日期。我有一个我认为正常工作的功能,直到我注意到如果一周中的某一天接近月末,比如二月,我得到了奇怪的数据。有谁知道发生了什么以及如何解决它?
function days(current) {
var week = new Array();
// Starting Monday not Sunday
var first = ((current.getDate() - current.getDay()) + 1);
for (var i = 0; i < 7; i++) {
week.push(
new Date(current.setDate(first++))
);
}
return week;
}
var input = new Date(2017, 1, 27);
console.log('input: %s', input);
var result = days(input);
console.log(result.map(d => d.toString()));
.as-console-wrapper{min-height:100%}
答案 0 :(得分:6)
如果你不想使用像Moment.js这样的其他类库,你也可以稍微改变一下你的功能,然后就可以了。试试这个:
function dates(current) {
var week= new Array();
// Starting Monday not Sunday
current.setDate((current.getDate() - current.getDay() +1));
for (var i = 0; i < 7; i++) {
week.push(
new Date(current)
);
current.setDate(current.getDate() +1);
}
return week;
}
console.log(dates(new Date(2017, 1, 27)));
&#13;
答案 1 :(得分:2)
我将使用您2017年2月27日的示例跟踪您的代码:
first = 27 - 1 + 1 = 27
loop:
Feb.setDate(27) = 27 feb
Feb.setDate(28) = 28 feb
Feb.setDate(29) = Not 29 days in Feb. So it sets current to 29-28 = 1st day of March
March.setDate(30) = March 30
March.setDate(31) = March 31
March.setDate(32) = Not 32 days in March. So it sets current to 31-32 = 1st of April..
April.setDate(33) = Not 33 days in April. So it sets current day 33-30 = 3rd day of May.
请注意我使用Month.setDate()
的简写来显示current
Date对象的月份。
所以问题在于您对当前使用的setDate的理解。它会更改月份,如果您使用的值不是该月的某一天,则会适当调整月份和日期。我希望这能为你解决问题。
有关如何将日期添加到日期,请参阅Add +1 to current date。根据您的代码,您可以将当前设置为一周的第一天,然后继续添加1天并将副本推送到数组:
function days(current) {
var week = [];
// Starting Monday not Sunday
var first = current.getDate() - current.getDay() + 1;
current.setDate(first);
for (var i = 0; i < 7; i++) {
week.push(new Date(+current));
current.setDate(current.getDate()+1);
}
return week;
}
var input = new Date(2017, 1, 27);
console.log('input: %s', input);
var result = days(input);
console.log(result.map(d => d.toString()));
&#13;
请注意,这会更改传入的日期(根据原始代码),您可能希望使当前副本以避免这种情况。
答案 2 :(得分:1)
您可以使用Moment.js库 - 实用程序库进行日期/时间操作
以下是从星期一开始获取当前一周的日期的示例代码:
function getThisWeekDates() {
var weekDates= [];
for (var i = 1; i <= 7; i++) {
weekDates.push(moment().day(i));
}
return weekDates;
}
var thisWeekDates = getThisWeekDates();
thisWeekDates.forEach(function(date){ console.log(date.format());});
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
&#13;
上面的代码将以下结果打印到控制台:
2017-03-20T21:26:27+01:00
2017-03-21T21:26:27+01:00
2017-03-22T21:26:27+01:00
2017-03-23T21:26:27+01:00
2017-03-24T21:26:27+01:00
2017-03-25T21:26:27+01:00
2017-03-26T21:26:27+02:00
答案 3 :(得分:1)
假设星期一从一周开始,您可以计算星期一并转到星期日。 getDate
为您提供星期几,而星期日从0开始。有了momnday,我们可以向前偏移6天以获取星期天
mondayThisWeek(date: Date): Date {
const d = new Date(date)
const day = d.getDay()
const diff = d.getDate() - day + (day === 0 ? -6 : 1)
return new Date(d.setDate(diff))
}
const offsetDate = (base: Date, count: number): Date => {
const date = new Date(base)
date.setDate(base.getDate() + count)
return date
}
thisWeek(today: Date): TimeRange {
const monday = mondayThisWeek(today)
return {
startDate: monday,
endDate: offsetDate(monday, 6)
}
}
答案 4 :(得分:0)
使用moment
即可轻松实现
const getWeekDates = () => {
let weekDates = [];
for (let i = 0; i < 7; i++)
weekDates.push(moment().add(i, 'd'));
return weekDates;
};
console.log(getWeekDates());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>