-1

I've got some dates I need to provide as args to my calendar

Say my holidays are

Start : 15/01/2015 ,
End: 25/01/2015

I need a way in which to pragmatically find the sunday before the start date (In this case 10/01/2015), Aswell as the week ending date which comes after my holidays end (31/02/2015) .

Any help would be much appreciated

Thanks!

does_not_compute
  • 476
  • 1
  • 7
  • 20
  • You should use the getDay() method: http://www.w3schools.com/jsref/jsref_getday.asp Note that sunday is 0, so you just need to compare the current day in a iteration or something like. – Dillinger Feb 24 '16 at 09:27
  • http://stackoverflow.com/questions/22144940/get-next-week-start-and-end-using-jquery-and-moment-js – Adam Feb 24 '16 at 09:28
  • @Dillenger This isn't very helpful for me sorry, this is assuming you are using todays date, I am not I would like to provide custom dates – does_not_compute Feb 24 '16 at 09:29
  • @Adam thanks for the link, If possible however I would like to do this in vanilla, it's for a work system which I don't think needs more libraries – does_not_compute Feb 24 '16 at 09:30
  • http://stackoverflow.com/questions/6024328/the-closest-sunday-before-given-date-with-javascript – undefined_variable Feb 24 '16 at 09:31
  • 1
    @does_not_compute "This isn't very helpful"? You can't replace `new Date()` with your custom date? Please don't expect a copy-paste answer to a question like this. Just use your brain, and apply the given tips. – Teemu Feb 24 '16 at 09:33
  • @Teemu I had no idea I could do that, I've come here for help stop being so rude. if dillenger had said I can provide my own date as an argument when instantiating a date object I would have known what to do, but he didn't.. I'm pretty amazed someone with as many points on this site believes that 1st answer to be a clear solution to my question when I have little experience working with JS dates. – does_not_compute Feb 24 '16 at 09:35
  • @Undefined_variable Perfect exactly what I was looking for, thank you very much :D – does_not_compute Feb 24 '16 at 09:36
  • 1
    Reading [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) might help? – Teemu Feb 24 '16 at 09:39
  • @Teemu thanks thats actually useful. I use w3schools a lot of time for docs but they miss a lot of the essentials clearly I can tell after reading that – does_not_compute Feb 24 '16 at 09:41
  • Well, you're not the only person who has found [w3schools](http://www.w3fools.com/) being not the best resource for learning. My comment above was not intended to be rude, I'm sorry if you've taken it as such. – Teemu Feb 24 '16 at 09:47

2 Answers2

0
var startDate = new Date("01/15/2015");
startDate.setDate(startDate.getDate() - startDate.getDay()-1);
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30
0

Try this:

var firstDayOfHolidays = new Date(); // You can set the date you want
var lastDayOfHolidays = new Date(); // You can set the date you want

var previousSunday = new Date(firstDayOfHolidays.getFullYear(), 
                              firstDayOfHolidays.getMonth(),         
                              firstDayOfHolidays.getDate() - firstDayOfHolidays.getDay());

var nextSunday = new Date(lastDayOfHolidays.getFullYear(), 
                          lastDayOfHolidays.getMonth(),
                          lastDayOfHolidays.getDate() - lastDayOfHolidays.getDay()+7);
alert( previousSunday );
alert( nextSunday );
Diego Vidal
  • 1,022
  • 12
  • 21