var range = getDates(new Date(), new Date().addDays(7));
我希望“range”成为一个日期对象的数组,其中包括两个日期之间每一天的日期。关键是它还应该处理月份和年份的边界。var range = getDates(new Date(), new Date().addDays(7));
我希望“range”成为一个日期对象的数组,其中包括两个日期之间每一天的日期。关键是它还应该处理月份和年份的边界。最近我在使用moment.js,以下方法可行:
function getDateRange(startDate, endDate, dateFormat) {
var dates = [],
end = moment(endDate),
diff = endDate.diff(startDate, 'days');
if(!startDate.isValid() || !endDate.isValid() || diff <= 0) {
return;
}
for(var i = 0; i < diff; i++) {
dates.push(end.subtract(1,'d').format(dateFormat));
}
return dates;
};
console.log(getDateRange(startDate, endDate, dateFormat));
["09/03/2015", "10/03/2015", "11/03/2015", "12/03/2015", "13/03/2015", "14/03/2015", "15/03/2015", "16/03/2015", "17/03/2015", "18/03/2015"]
moment().format()
应该仅用于视图中的显示目的。同时请记住,moment().clone()
确保与输入参数分离,这意味着输入日期不会被更改。我强烈建议您在处理日期时使用 moment.js。startDate
、endDate
参数的值interval
参数是可选的,默认为“days”。使用由 .add()
方法(moment.js)支持的间隔。 更多详细信息在此处total
参数很有用。它默认为 1。var startDate = moment(),
endDate = moment().add(1, 'days');
getDatesRangeArray(startDate, endDate, 'minutes', 30);
功能:
var getDatesRangeArray = function (startDate, endDate, interval, total) {
var config = {
interval: interval || 'days',
total: total || 1
},
dateArray = [],
currentDate = startDate.clone();
while (currentDate < endDate) {
dateArray.push(currentDate);
currentDate = currentDate.clone().add(config.total, config.interval);
}
return dateArray;
};
var start = new Date("01/05/2017");
var end = new Date("06/30/2017");
var newend = end.setDate(end.getDate()+1);
end = new Date(newend);
while(start < end){
console.log(new Date(start).getTime() / 1000); // unix timestamp format
console.log(start); // ISO Date format
var newDate = start.setDate(start.getDate() + 1);
start = new Date(newDate);
}
var listDate = [];
var startDate ='2017-02-01';
var endDate = '2017-02-10';
var dateMove = new Date(startDate);
var strDate = startDate;
while (strDate < endDate){
var strDate = dateMove.toISOString().slice(0,10);
listDate.push(strDate);
dateMove.setDate(dateMove.getDate()+1);
};
console.log(listDate);
//["2017-02-01", "2017-02-02", "2017-02-03", "2017-02-04", "2017-02-05", "2017-02-06", "2017-02-07", "2017-02-08", "2017-02-09", "2017-02-10"]
strDate
前面加上 var
! - Boris YakubchikArray(7).fill().map((_,i) => dayjs().subtract(i, "day").format("YYYY-MM-DD"));
[...Array(7)].map((_, i) => startOfWeek.add(i, 'day'))
,其中startOfWeek是一个dayjs对象,例如dates.get(date).startOf('week').subtract(-1, 'week')
。 - luzmcosta这是使用 date-fns 库的另一种几行代码的解决方案:
const { format, differenceInDays, addDays } = dateFns;
const getRangeDates = (startDate, endDate) => {
const days = differenceInDays(endDate, startDate);
console.log([...Array(days + 1).keys()].map((i) => format(addDays(startDate, i), 'YYYY-MM-DD')));
};
getRangeDates('2021-06-01', '2021-06-05');
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js"></script>
使用JavaScript
const getDatesBetween = (startDate, endDate, includeEndDate) => {
const dates = [];
const currentDate = startDate;
while (currentDate < endDate) {
dates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
if (includeEndDate) dates.push(endDate);
return dates;
};
使用TypeScript
const getDatesBetween = (
startDate: Date,
endDate: Date,
includeEndDate?: boolean
) => {
const dates = [];
const currentDate = startDate;
while (currentDate < endDate) {
dates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
if (includeEndDate) dates.push(endDate);
return dates;
};
例子
console.log(getDatesBetween(new Date(2020, 0, 1), new Date(2020, 0, 3)));
console.log(getDatesBetween(new Date(2020, 0, 1), new Date(2020, 0, 3), true));
function datesArray(start, end) {
let result = [], current = new Date(start);
while (current <= end)
result.push(current) && (current = new Date(current)) && current.setDate(current.getDate() + 1);
return result;
}
用法
function datesArray(start, end) {
let result = [], current = new Date(start);
while (current <= end)
result.push(current) && (current = new Date(current)) && current.setDate(current.getDate() + 1);
return result;
}
// Usage
const test = datesArray(
new Date('2020-02-26'),
new Date('2020-03-05')
);
for (let i = 0; i < test.length; i ++ ) {
console.log(
test[i].toISOString().slice(0,10)
);
}
这里有一行代码,不需要任何库,以防您不想创建另一个函数。只需用您的变量或日期值替换startDate(两个位置)和endDate(它们是js日期对象)。当然,如果您喜欢,可以将其包装在一个函数中。
Array(Math.floor((endDate - startDate) / 86400000) + 1).fill().map((_, idx) => (new Date(startDate.getTime() + idx * 86400000)))
我使用这个函数
function getDatesRange(startDate, stopDate) {
const ONE_DAY = 24*3600*1000;
var days= [];
var currentDate = new Date(startDate);
while (currentDate <= stopDate) {
days.push(new Date (currentDate));
currentDate = currentDate - 1 + 1 + ONE_DAY;
}
return days;
}
eachDayOfInterval
[1]和addDays
[2]来返回开始日期和结束日期之间的日期数组。 - tinystone