0

I want to get start and enddates for upcoming 4 weeks(only weekdays).

Suppose today's date is 2015-12-01 then the result should be as below

Week0 will have StartDate = 2015-11-30 and EndDate = 2015-12-04
Week1 will have StartDate = 2015-12-07 and EndDate = 2015-12-11
Week2 will have StartDate = 2015-12-14 and EndDate = 2015-12-18
Week3 will have StartDate = 2015-12-21 and EndDate = 2015-12-25

Here date of Week0 should be calculated from current date.

Techy
  • 41
  • 4
  • 1
    Anything to do with dates, my go-to is [Moment.js](http://momentjs.com/), it's a pretty comprehensive library for manipulating dates. – jonny Dec 01 '15 at 10:12
  • 3
    Looks like a homework. What you have tried? – Vladimirs Dec 01 '15 at 10:13
  • Possible duplicate of [How to get first and last day of the week in JavaScript](http://stackoverflow.com/questions/5210376/how-to-get-first-and-last-day-of-the-week-in-javascript) – Alexander Dec 30 '15 at 09:31

2 Answers2

0

Try the moment library. It's pretty easy to use, so you should be able to figure out quickly, how to do this.

var date = moment(yourdate);
// iterate by date.add(1, "week")
var start = date.clone().startOf('week');
var end = date.clone().endOf('week');
//use .format('YYYY-MM-DD'); to print out
pkopac
  • 986
  • 1
  • 13
  • 21
  • This fails to answer the question. It would be better served as a comment. – jonny Dec 01 '15 at 10:16
  • While I'm sure the moment library is great, if you're going to recommend that they use a specific tool, library, etc. to solve their problem you also need to actually explain how they can use it to solve their problem. – Anthony Grist Dec 01 '15 at 10:17
  • @ᴊᴏɴᴀᴛʜᴀɴʙʀᴏᴏᴋs OK guys, I've added the solution in abstract code, I seriously hope everyone can figure out, how to make a for cycle ;) – pkopac Dec 01 '15 at 10:25
  • I need to do this in Core Javascript only. – Techy Dec 01 '15 at 10:37
  • @Techy Well, then it certainly is a homework and you shall do it yourself :) Refer to JS Date API documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – pkopac Dec 01 '15 at 10:47
0

Here is how you would use the Moment.js library (as mentioned in the comments) to achieve the output you desire. It's quite easy by using the built in functions (to see the result, hit F12 on your keyboard or open the console some other way)

var weeks = 4;

for (var i = 0 ; i < weeks ; i++) {
    var start = moment().startOf('isoweek').add(i, 'weeks');
    var end = start.clone().add(4, 'days');
    console.log("Week%d will have StartDate = %s and EndDate = %s", i, start.format('YYYY-MM-DD'), end.format('YYYY-MM-DD'));
}
<script src="http://momentjs.com/downloads/moment.min.js"></script>

Couple simple built in functions at work here, namely:

  • moment, which is an instance of the moment class - essentially a datetime string.
  • startOf, pretty self explanatory, finds the exact datetime of when (in this case) the start of the week was
  • add, which adds a certain amount of x i.e. days, weeks, months etc. to the moment instance
  • clone, a necessary step which clones the original moment to prevent it from being modified by the end variable.
  • and format, pretty obvious, formats the moment based on the string given as its argument.

Take a look at the Moment.js docs and have a little decipher of the code; it will help you understand Moment.js as a library much better. Hope this helps.

jonny
  • 3,022
  • 1
  • 17
  • 30