Reputation: 914
const from = "2020-07-08";
const to = "2020-07-14";
How can I get the date of, let's say, "Thursday" inside a given week using moment.js? In this example below, the expected answered would be "2020-07-09" for "Thursday" and "2020-07-12" for "Sunday" and so on.
Upvotes: 1
Views: 136
Reputation: 5858
let findNextWeekDay = (input_date,day)=> {
let the_date = moment(input_date);
if (the_date.day(day).isSameOrBefore(input_date)) {
return the_date.day(day).add(7,'days');
}
return the_date.day(day)
}
console.log(findNextWeekDay("2021-06-20","Wednesday"));
console.log(findNextWeekDay("2021-06-22","Wednesday"));
console.log(findNextWeekDay("2021-06-23","Wednesday"));
console.log(findNextWeekDay("2021-06-26","Wednesday"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>
Upvotes: 2
Reputation: 511
var new_date = moment("2020-07-08", "YYYY-MM-DD").add(5, 'days');
Upvotes: 2