I need to list week array from 2017 till now. My week start from Monday and ends with Sunday I tried to do in moment.js,
const startDate = moment().isoWeekday('Monday').format('DD-MM-YYYY');
const endDate = moment().isoWeekday('Sunday').format('DD-MM-YYYY');
I tried to like this to get the week start date and end date but I don't know the process to next step.
Note my array list start with ["02-01-2017", "08-01-2017"]
and ends with current week will be the Last one.
const result = [["02-01-2017", "08-01-2017"],...... ["05-01-2018", "11-01-2018"]]
I need to list week array from 2017 till now. My week start from Monday and ends with Sunday I tried to do in moment.js,
const startDate = moment().isoWeekday('Monday').format('DD-MM-YYYY');
const endDate = moment().isoWeekday('Sunday').format('DD-MM-YYYY');
I tried to like this to get the week start date and end date but I don't know the process to next step.
Note my array list start with ["02-01-2017", "08-01-2017"]
and ends with current week will be the Last one.
const result = [["02-01-2017", "08-01-2017"],...... ["05-01-2018", "11-01-2018"]]
Share
Improve this question
edited Feb 6, 2018 at 7:37
CommunityBot
11 silver badge
asked Feb 6, 2018 at 6:16
6round6round
1825 silver badges18 bronze badges
4
- ["02-01-2017", "08-01-2017"] is this you input? – rijin Commented Feb 6, 2018 at 7:27
- @rijin thank you reply no it not my input, I need to get the start week to date and end week date from 2017 till current week, i showed you the start date array of 2017 – 6round Commented Feb 6, 2018 at 7:31
- so, inputs are 2017 and monday => returns all mondays till today from 2017? – rijin Commented Feb 6, 2018 at 7:33
-
@rijin my week start with
Monday
and end withSunday
. I need all the week start date and end date till current week this will be my resultconst result = {["02-01-2017", "08-01-2017"] .... ["05-01-2018", "11-01-2018"]}
– 6round Commented Feb 6, 2018 at 7:37
2 Answers
Reset to default 8You can initialize your start date to 1st weekday of 1st January 2017 and iterate to all the dates less than today and keep adding days in an array.
var weeks = [];
var startDate = moment(new Date(2017,0,1)).isoWeekday(8);
if(startDate.date() == 8) {
startDate = startDate.isoWeekday(-6)
}
var today = moment().isoWeekday('Sunday');
while(startDate.isBefore(today)) {
let startDateWeek = startDate.isoWeekday('Monday').format('DD-MM-YYYY');
let endDateWeek = startDate.isoWeekday('Sunday').format('DD-MM-YYYY');
startDate.add(7,'days');
weeks.push([startDateWeek,endDateWeek]);
}
console.log(weeks)
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.20.1/moment.min.js"></script>
Something like that would help?
let startDate = moment('2017-01-01').startOf('week').format('YYYY-MM-DD');
let endDate = moment(new Date()).startOf('week').format('YYYY-MM-DD');
const weeks = [];
while (startDate <= endDate) {
weeks.push(startDate);
startDate = moment(startDate).add(7, 'days').format('YYYY-MM-DD');
}
console.log('weeks:', weeks);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742302251a4418244.html
评论列表(0条)