Search code examples
javascriptcountmonthcalendar

Count on the month


I have a program which I work with, I use it to add hours of the employees. But now I have a problem, I have set the date automatically on the Monday of past week (28-09-2015 Monday, 29-09-2015 Tuesday etc.), but when I'm at the end of the month, it doesn't work anymore.

Here you can see a picture:

When we go from 30 September to 1 October, it stopped working. This is the script I use

$(document).ready(function() {
function getMonday(d) 
{
var day = d.getDay();
diff = d.getDate() - day + -6; 
return new Date(d.setDate(diff));
}

var day = getMonday(new Date());
var month = day.getMonth()+1;

for(var i = 0; i < 7; i++)
{
$('[name="start_day'+i+'"').val(day.getDate()+i);
$('[name="start_month'+i+'"').val(month);
}

and this is the Javascript I use for the date:

<script src = "https://code.jquery.com/jquery-1.9.1.min.js"> </script>

Can anybody please tell me what I'm doing wrong?


Solution

  • Try it like this. Increase the time by 24 hours...

    $(document).ready(function() {
    function getMonday(d) 
    {
    var day = d.getDay();
    diff = d.getDate() - day + -6; 
    return new Date(d.setDate(diff));
    }
    
    var day = getMonday(new Date());
    var month = day.getMonth()+1;
    
    for(var i = 0; i < 7; i++)
    {
    var thisDate = new Date(day.getTime() + (24 * 60 * 60 * 1000 * i)); 
    $('[name="start_day'+i+'"').val(thisDate.getDate());
    $('[name="start_month'+i+'"').val(thisDate.getMonth() + 1);
    }