Skip to content
Advertisement

someone help me how to use if if else in java script using get current date

this is my code someone can help me i cant use if else on get current date i try all google tutorial no one work for me please good help for me if someone help me thank you in advance the only problem if else if i run this code if else wont read it

<script type="text/javascript">
  function calc() {


    var today = new Date();
    var month = today.getMonth(); // Returns 9
    console.log(month); // Output: 9


    var textValue3 = document.getElementById('input3').value;
    var textValue2 = document.getElementById('input2').value
    var textValue1 = document.getElementById('input1').value;
    var basic = 5;
    if (month = '1') {
      var rate_interest = 0;
    }
    else if (month = '2') {
      var rate_interest = 0;
    }
    else if (month = '3') {
      var rate_interest = 0.06;
    }
    else if (month = '4') {
      var rate_interest = 0.08;
    }
    else if (month = '5') {
      var rate_interest = 0.10;
    }
    else if (month = '6') {
      var rate_interest = 0.12;
    }
    else if (month = '7') {
      var rate_interest = 0.14;
    }
    else if (month = '8') {
      var rate_interest = 0.16;
    }
    else if (month = '9') {
      var rate_interest = 0.18;
    }
    else if (month = '10') {
      var rate_interest = 0.20;
    }
    else if (month = '11') {
      var rate_interest = 0.22;
    }
    else if (month = '12') {
      var rate_interest = 0.24;
    }








    document.getElementById('output').value = (basic) + (textValue1 / 1000) + (textValue2 / 1000) + (textValue3 / 1000) + (basic * rate_interest);
  }
</script>

Advertisement

Answer

In the ‘if condition’ you need to write == instead of =

date.getMonth() return the month in 0 to 11 so you need to plus one in a month.

    function calc() {


    var today = new Date();
    var month = today.getMonth(); // Returns 9
    month = month + 1;
    console.log(month); // Output: 9


    var textValue3 = document.getElementById('input3').value;
    var textValue2 = document.getElementById('input2').value
    var textValue1 = document.getElementById('input1').value;
    var basic = 5;
    var rate_interest;
    if (month == 1) {
       rate_interest = 0;
    }
    else if (month == 2) {
       rate_interest = 0;
    }
    else if (month == 3) {
       rate_interest = 0.06;
    }
    else if (month == 4) {
       rate_interest = 0.08;
    }
    else if (month == 5) {
       rate_interest = 0.10;
    }
    else if (month == 6) {
       rate_interest = 0.12;
    }
    else if (month == 7) {
       rate_interest = 0.14;
    }
    else if (month == 8) {
       rate_interest = 0.16;
    }
    else if (month == 9) {
       rate_interest = 0.18;
    }
    else if (month == 10) {
       rate_interest = 0.20;
    }
    else if (month == 11) {
       rate_interest = 0.22;
    }
    else if (month == 12) {
       rate_interest = 0.24;
    }
    document.getElementById('output').value = (basic) + (textValue1 / 1000) + (textValue2 / 1000) + (textValue3 / 1000) + (basic * rate_interest);
  }
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement