-1

I am developing a system about Allowance in PHP, MySQL and it should calculate the last date employee to receive their allowance. This is my JavaScript to calculate the last date. Output for below code is 15-7-2015. I would like it to be 15-07-2015.

What should I change to get output like that ?

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

 var x = document.form1.tempoh.value; //<-----------Tempoh Elaun
    var CurrentDate = new Date();
    CurrentDate.setMonth(CurrentDate.getMonth() + eval(x));

    var day = CurrentDate.getDate();
    var monthIndex = CurrentDate.getMonth()+1;
    var year = CurrentDate.getFullYear();

 document.getElementById('tamatElaun').value = day + "-" + monthIndex + "-" + year;
    //document.write(day, monthNames[monthIndex], year);
}
</script>
Cool Blue
  • 6,438
  • 6
  • 29
  • 68
Mazrin
  • 9
  • 3
  • 1
    possible duplicate of [How do I get Month and Date of JavaScript in 2 digit format?](http://stackoverflow.com/questions/6040515/how-do-i-get-month-and-date-of-javascript-in-2-digit-format) OR [Javascript add leading zeroes to date](http://stackoverflow.com/questions/3605214/javascript-add-leading-zeroes-to-date) OR [Javascript date - Leading 0 for days and months where applicable](http://stackoverflow.com/questions/6979697/javascript-date-leading-0-for-days-and-months-where-applicable) – Sean Jul 15 '15 at 04:43

4 Answers4

0

Try this

 var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!

    var yyyy = today.getFullYear();
    if(dd<10){
        dd='0'+dd
    } 
    if(mm<10){
        mm='0'+mm
    } 
    var today = dd+'-'+mm+'-'+yyyy;
    document.getElementById("tamatElaun").value = today;
Mukesh Kalgude
  • 4,814
  • 2
  • 17
  • 32
0

Try my code

var CurrentDate = new Date('2015-7-15');
var listMonths = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
var day = CurrentDate.getDate();
var monthIndex = CurrentDate.getMonth();
var year = CurrentDate.getFullYear();
alert(day + '/' + listMonths[monthIndex] + '/' + year);

You can also change listMonths into array of string, ex: ['Jan', 'Feb', ..., 'Dec']

Thi Tran
  • 681
  • 5
  • 11
0

Please try like this,

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

    var x = document.form1.tempoh.value; //<-----------Tempoh Elaun
    var CurrentDate = new Date();
    CurrentDate.setMonth(CurrentDate.getMonth() + eval(x));

    var day = CurrentDate.getDate();
    var monthIndex = ("0" + (CurrentDate.getMonth() + 1)).slice(-2);
    var year = CurrentDate.getFullYear();

    document.getElementById('tamatElaun').value = day + "-" + monthIndex + "-" + year;
    //document.write(day, monthNames[monthIndex], year);
}
</script>
US-1234
  • 1,519
  • 4
  • 24
  • 61
0
 var CurrentDate = new Date();
    CurrentDate.setMonth(CurrentDate.getMonth());

    var day = CurrentDate.getDate();
    var monthIndex = CurrentDate.getMonth()+1;
    if(monthIndex<10){
        monthIndex=('0'+monthIndex);
    }
    var year = CurrentDate.getFullYear();
    alert(monthIndex);
    //document.getElementById('tamatElaun').value = day + "-" + monthIndex + "-" + year;
    });
  • only add this code. if(monthIndex<10){ monthIndex=('0'+monthIndex); } –  Jul 15 '15 at 04:51