Count on the month

孤人 提交于 2019-12-11 04:04:54

问题


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?


回答1:


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);
}



回答2:


Below will work, fiddler

$(document).ready(function() {
function getMonday(d) 
{

var day1 = d.getDay();
diff = d.getDate() - day1 + -6; 
return new Date(d.setDate(diff));
}

var day1 = getMonday(new Date());
var month1 = day1.getMonth()+1;

for(var i = 0; i < 7; i++)
{
    var dat = new Date();
    dat.setDate(day1.getDate() + i);
$('[name="start_day'+i+'"').val(dat);
$('[name="start_month'+i+'"').val(dat.getMonth());
}
});


来源:https://stackoverflow.com/questions/32948040/count-on-the-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!