0

Hello I just need another set of eyes on this. I created this code to set a calendar with the current dates on it, it was working fine but today, sat jan 30, the calendar was still showing it start at sat jan 23.

here is the link to my codepen of it: http://codepen.io/cavascript/pen/MKXrbj

here is the code:

 var date = new Date(); var saturday = date.getDate() - date.getDay() - 1; var sunday = date.getDate() - date.getDay() ; var monday = date.getDate() - date.getDay() + 1 ; var tuesday = date.getDate() - date.getDay() + 2 ; var wednesday= date.getDate() - date.getDay() + 3 ; var thursday = date.getDate() - date.getDay() + 4 ; var friday = date.getDate() - date.getDay() + 5 ; var saturday = (new Date(date.setDate(saturday))).toDateString(); var sunday = (new Date(date.setDate(sunday))).toDateString(); var monday = (new Date(date.setDate(monday))).toDateString(); var tuesday = (new Date(date.setDate(tuesday))).toDateString(); var wednesday = (new Date(date.setDate(wednesday))).toDateString(); var thursday = (new Date(date.setDate(thursday))).toDateString(); var friday = (new Date(date.setDate(friday))).toDateString(); //add to html row $('#dateRow').html('<td colspan="4">Date</td><td colspan="4">'+saturday+'</td><td colspan="4">'+sunday+'</td><td colspan ="4">'+monday+'</td><td colspan="4">'+tuesday+'</td><td colspan="4">'+wednesday+'</td><td colspan="4">'+thursday+'</td><td colspan="4">'+friday+'</td><td>Hours</td><td>Delete</td>'); 

And here is the HTML:

 <div class="container-fluid"> <div class="table-responsive"> <table class="table table-bordered"> <tbody> <tr id="dateRow"> </tr> <tr> <td colspan="4">Name</td> <td colspan="2">Br/lnch</td> <td colspan="2">Dinner</td> <td colspan="2">Br/lnch</td> <td colspan="2">Dinner</td> <td colspan="2">Br/lnch</td> <td colspan="2">Dinner</td> <td colspan="2">Br/lnch</td> <td colspan="2">Dinner</td> <td colspan="2">Br/lnch</td> <td colspan="2">Dinner</td> <td colspan="2">Br/lnch</td> <td colspan="2">Dinner</td> <td colspan="2">Br/lnch</td> <td colspan="2">Dinner</td> <td>&nbsp</td> <td>&nbsp</td> </tr> </tbody> </table> </div> 

Thank you for any input

3
  • 1
    Should this table always start one week before the current date? Commented Jan 30, 2016 at 19:33
  • 2
    this logic doesn't make sense date.getDate() - date.getDay() - 1 ...do the math ... 30-6-1 = 23 Commented Jan 30, 2016 at 19:37
  • No it shouldn't start the week before current date, and you're right the logic doesn't make sense. It should start at the saturday of that current week. Sorry guys I was just scatter brained Commented Jan 30, 2016 at 19:47

1 Answer 1

1

Your code

date.getDate() - date.getDay() - 1; 

will return 23 as we are presently on 30/01/2016 - so it does exactly what you asked it to do :)

Check out Date.js or Moment.js which have a great many helper methods to make Datetime arithmetic much easier.

I guess you don't want the week shown to not always be from Saturday 23rd to Fri 29th.

Possible solution: call getDate() check if result is a Saturday use this as var saturday and from there you can deduce Monday - Friday. However, if getDate() is not a Saturday find previous saturday and use that.

Also, a useful resource for calendars

Have a go - if you can't do it then pop me a message and I'll gladly help out.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.