Reputation: 59
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> </td>
<td> </td>
</tr>
</tbody>
</table>
</div>
Thank you for any input
Upvotes: 0
Views: 61
Reputation: 407
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.
Upvotes: 1