0

Okay basicly,I'm making a program wich take the date of today, and give back the date of the last day of the previous month. I take into account if the year is leap or not, but for some reason when I do if month = 2 and set day = 29, the variable dd store mm = 2, and I get it in the result, where I shouldn't cus we're in May

function IsBi(annee) {

if ((annee%4==0) && ((annee%100!=0) || (annee%400==0))) return true;
else return false;

}

var dt = new Date(); 
dt.setDate(1);       
dt.setHours(-1);     
var joursei = dt
var today = new Date(joursei);
var dd=    today.getDate(joursei);
var mm=  today.getMonth(joursei)+1;
var yyyy= today.getFullYear(joursei);
var zig = IsBi(yyyy);
if (zig = true) {
        //if(dd<10)  {dd='0'+dd;}
        //if(mm<10){mm='0'+mm;}
        if (mm = 2) {
            dd = 29;
        }
{
    necResult=yyyy+'/'+mm+'/'+dd;}
}

else if (zig = false) {

    //if(dd<10)  {dd='0'+dd;}
    //if(mm<10){mm='0'+mm;}
    if (mm = 2) {
        dd= 28;
    }


    {
    necResult = yyyy +"/"+mm+"/"+dd;
    }

}
alert(necResult);

Mitch
  • 29
  • 6

1 Answers1

-1

You're doing assignment in your conditionals.

Instead, check for equality using the comparison operator.

if (zig === true) {
        //if(dd<10)  {dd='0'+dd;}
        //if(mm<10){mm='0'+mm;}
        if (mm = 2) {
            dd = 29;
        }
{
    necResult=yyyy+'/'+mm+'/'+dd;}
}

else if (zig === false) {

    //if(dd<10)  {dd='0'+dd;}
    //if(mm<10){mm='0'+mm;}
    if (mm === 2) {
        dd= 28;
    }


    {
    necResult = yyyy +"/"+mm+"/"+dd;
    }

}
Lee Brindley
  • 6,242
  • 5
  • 41
  • 62