1

I ve done a function that change date if it is a weekend but Instead of giving me the next day it do to me day+1 for example I had an input with the 31-09-2012 wich was the last sunday.

I have my function

<script type="text/javascript">
    function getdate2() {
        var items = new Array();
        var itemCount = document.getElementsByClassName("datepicker hasDatepicker");

        for (var i = 0; i < itemCount.length; i++) {
            items[i] = document.getElementById("date" + (i + 1)).value;
        }



        for (var i = 0; i < itemCount.length; i++) {
            items[i] = document.getElementById("date" + (i + 1)).value;
            var itemDtParts = items[i].split("-");
            var itemDt = new Date(itemDtParts[2], itemDtParts[1] - 1, itemDtParts[0]);
                    if (itemDt.getDay() == 6) {

                itemCount[i].value = (itemDt.getDate() < 9 ? "0" : "")+ (itemDt.getDate()+2)+ "-" + (itemDt.getMonth() < 9 ? "0" : "") + (itemDt.getMonth() + 1) + "-" + itemDt.getFullYear();


            }
                        if (itemDt.getDay() == 0) {

               itemCount[i].value = (itemDt.getDate() < 9 ? "0" : "")+ (itemDt.getDate()+1)+ "-" + (itemDt.getMonth() < 9 ? "0" : "") + (itemDt.getMonth() + 1) + "-" + itemDt.getFullYear();


            }

        }
       return items;
       }
</script>

But Instead of giving me the first october it had give me the 31-09-2012 which does not exist.

I really do not know how to process.

Sam
  • 7,252
  • 16
  • 46
  • 65
Stanislas Piotrowski
  • 2,595
  • 10
  • 40
  • 59

3 Answers3

1

You need to store the maximum number of days per month in array.

var dayspermonth = new array(31,28,31,30,31,30,31,31,30,31,30,31);

Best to do it in a seperate function:

function int lastDay(d, m){
    var result = d
    var dayspermonth = new array(31,28,31,30,31,30,31,31,30,31,30,31);
    if(d > dayspermonth[m]){
        d=1;
    }
    return d;
}

Then you just have to do (for example):

itemCount[i].value = (itemDt.getDate() < 9 ? "0" : "")+ lastDay((itemDt.getDate()+1), itemDt.getMonth())+ "-" + (itemDt.getMonth() < 9 ? "0" : "") + (itemDt.getMonth() + 1) + "-" + itemDt.getFullYear();

Edit: You need to increase the month by 1 if that happens. And you need to add 1 to year and set month to 1 if the old date was 31-12-YYYY

LuigiEdlCarno
  • 2,410
  • 2
  • 21
  • 37
  • but it can change all the years – Stanislas Piotrowski Oct 01 '12 at 13:16
  • Leap year to consider as well. – Scott Oct 01 '12 at 13:20
  • I don't know, if there is a class in js, that provides a check, whether a year is a leap year, but you could write that function yourself. Check [link](http://en.wikipedia.org/wiki/Leap_year#Algorithm) for the algorith and ceate a function that builds and returns your dayspermontharray according to the year in your date – LuigiEdlCarno Oct 01 '12 at 13:43
1

try date.js easy to find weekend

Date.today().is().weekday() // Is today a weekday?

Sender
  • 6,660
  • 12
  • 47
  • 66
  • 1
    I concur that this is best done using a library instead of hacking away with loops over dates. – Henrik Mühe Oct 01 '12 at 13:18
  • I'm not looking to check if a date is a weekend the trouble was to change date, because I know how to check if it is weekend but when I tried to change date it just give me day+1 without checking if the month is over. – Stanislas Piotrowski Oct 01 '12 at 13:59
0

2012/07/25 - Saturday. Code check if its Saturday - add 2 days, if its Sunday - add 1 day http://jsfiddle.net/S3Q4P/

var mydate=new Date(2012,07,25)
if(mydate.getDay() == 6) //Saturday
mydate =new Date(mydate.getFullYear(),mydate.getMonth(),mydate.getDate()+2)
else if (mydate.getDay() == 0) //Sunday
     mydate =new Date(mydate.getFullYear(),mydate.getMonth(),mydate.getDate()+1)
Alex
  • 329
  • 2
  • 17