deg*_*ath 2 javascript date typescript
I try to create a list of days between two days.
I created solution like this:
this.daysBetween = [];
while (dateFrom.getDate() !== dateTo.getDate()+1 ) {
this.daysBetween.push(this.dateFrom);
dateFrom.setDate(dateFrom.getDate()+1);
}
Run Code Online (Sandbox Code Playgroud)
But it works only in 90% cases (if there is month change it doesn't work)
e.g. when I pick dates:
dateFrom: 29 august
dateTo: 30 august
Run Code Online (Sandbox Code Playgroud)
it prints me days from 29 august till 30 september ignoring 31 august ...
Any ideas how to fix my solution, or maybe there is better one?
EDIT:
My question is different than question suggested, because in my question I have as input two dates
e.g.
let dateFrom = new Date(2018, 9, 29);
let dateTo = new Date(2018, 9, 30);
Run Code Online (Sandbox Code Playgroud)
On suggested duplicate result of this could have been int number 1
My question is how to loop through all days between two dates (dateFrom, dateTo)
Where result of those 2 dates examples (dateFrom, dateTo)
would've been list with 2 elements:
Mon Oct 29 2018 00:00:00 GMT+0100
Tue Oct 30 2018 00:00:00 GMT+0100
Run Code Online (Sandbox Code Playgroud)
您可以使用时刻的持续时间:
/**
* Get an array of moment instances, each representing a day beween given timestamps.
* @param {string|Date} from start date
* @param {string|Date} to end date
*/
function daysBetween(from, to) {
const fromDate = moment(new Date(from)).startOf('day');
const toDate = moment(new Date(to)).endOf('day');
const span = moment.duration(toDate.diff(fromDate)).asDays();
const days = [];
for (let i = 0; i <= span; i++) {
days.push(moment(fromDate).add(i, 'day').startOf('day'));
}
return days;
}
const days = daysBetween('29-Aug-2018', '30-Sep-2018');
console.info(days.map(d => d.toString()).join('\n'));
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
您可以更改循环开始/结束条件以包括/排除第一天/最后一天。
更新:使用Luxon代替:
const DateTime = luxon.DateTime;
function daysBetween(start,end){
dateStart = DateTime.fromJSDate(start);
dateEnd = DateTime.fromJSDate(end);
diffDays = dateEnd.diff(dateStart,'days');
const days = [];
for(let i = 0 ; i < diffDays.days; i++){
days.push(new Date(dateStart.plus({days:i+1}).toMillis()));
}
return days;
}
console.log(daysBetween(new Date('23-Feb-2020'),new Date('5-Mar-2020')));
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/npm/luxon@2.3.0/build/global/luxon.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
下面给出了简单的 Typescript 解决方案
class MyDate {
dates: Date[];
constructor() {
this.dates = [];
}
private addDays(currentDate) {
let date = new Date(currentDate);
date.setDate(date.getDate() + 1);
return date;
}
getDates(startDate: Date, endDate: Date) {
let currentDate: Date = startDate;
while (currentDate <= endDate) {
this.dates.push(currentDate);
currentDate = this.addDays(currentDate);
}
return this.dates;
}
}
let md = new MyDate();
let daysBetween: Date[] = md.getDates(new Date(2018, 7, 22), new Date(2018, 8, 30));
console.log(daysBetween);
Run Code Online (Sandbox Code Playgroud)
您可以计算以毫秒为单位的差异,然后将其转换为以天为单位的差异。
然后,您可以使用它来用 Date 对象填充数组:
const MS_PER_DAY: number = 1000 x 60 x 60 x 24;
const start: number = dateFrom.getTime();
const end: number = dateTo.getTime();
const daysBetweenDates: number = Math.ceil((end - start) / MS_PER_DAY);
// The days array will contain a Date object for each day between dates (inclusive)
const days: Date[] = Array.from(new Array(daysBetweenDates + 1),
(v, i) => new Date(start + (i * MS_PER_DAY)));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6189 次 |
最近记录: |