Reputation: 643
I have start and end dates in my component which I am getting using moment
library through the time picker.
So suppose my datepicker selects startDate and endDate for the today. Now I need another dates let's say previousStartDate
and previousEndDate
which should be the same time difference compared to startDate
and endDate
.
startDate = '20-06-2019 00:00:00'
endDate = '20-06-2019 23:59:59'
So the previousStartDate
and previousEndDate
should be
previousStartDate = '19-06-2019 00:00:00'
previousEndDate = '19-06-2019 23:59:59'
How can do that?
I have tried this but with no success
getPreviousStartDates ({ startDate, endDate }) {
const diff = endDate.diff(startDate)
const diffDuration = moment.duration(diff)
const subtractStartDate = startDate.subtract(diff)
console.log({subtractStartDate})
return subtractStartDate
}
Some more clarification
Suppose The time difference between the startDate
and endDate
is 3 days (17-6-2019 00:00:00 && 19-6-2019 23:59:59)
then the time difference between previousStartDate
and previousEndDate
should be the same but past 3 days. (14-6-2019 00:00:00 && 16-6-2019 23:59:59)
Upvotes: 0
Views: 135
Reputation: 951
Is it a requirement that it is done using momentjs? I'm not so familiar with momentjs, but you could do it in javascript like this:
let start = new Date("2019-06-20 00:00:00");
let end = new Date("2019-06-20 23:59:59");
let duration = end.getTime() - start.getTime(); // milliseconds
let prevStart = new Date(start.getTime() - duration);
let prevEnd = new Date(prevStart.getTime() + duration);
Upvotes: 1