简体   繁体   中英

Format changing in adding dates function javascript

I am using a Javascript function, which lets to add days to the current date, but I am facing problem when i tried to change the Date format, here is my working code,

  <body onload="addDate();">
         <br/>
        <h1>Adding number of days to current date in Javascript</h1>
        Today's Date (MM / DD / YYYY) : <input type="text" id="date1" readonly/> <br/> <br/>
        Number of days to add : <input type="text" id="days" onChange="datechange()" /> <br/> <br/>
        New Date (MM / DD / YYYY) : <input type="text" id="date2" readonly/>
        <script type="text/javascript">
              function datechange()
              {
                    var d = document.getElementById('days').value;
                    var myDate = new Date(document.getElementById('date1').value);
                    myDate.setDate(myDate.getDate() + parseInt(d));
                    document.getElementById('date2').value = (myDate.getMonth() + 1) + '/' + (myDate.getDate()) + '/' + (myDate.getFullYear());
              }
              function addDate()
              {
                     date = new Date();
                     var month = date.getMonth()+1;
                     var day = date.getDate();
                     var year = date.getFullYear();
                     if (document.getElementById('date1').value == '')
                     {
                           document.getElementById('date1').value = month + '/' + day + '/' + year;
                     }
              }
        </script>

I tried to change the date format to DD/MM/YYYY,with this code,

  <script type="text/javascript">
              function datechange()
              {
                    var d = document.getElementById('days').value;
                    var myDate = new Date(document.getElementById('date1').value);
                    myDate.setDate(myDate.getDate() + parseInt(d));
                    document.getElementById('date2').value = myDate.getDate() + '/' + (myDate.getMonth() + 1) + '/' + (myDate.getFullYear());
              }
              function addDate()
              {
                     date = new Date();
                     var month = date.getMonth()+1;
                     var day = date.getDate();
                     var year = date.getFullYear();
                     if (document.getElementById('date1').value == '')
                     {
                           document.getElementById('date1').value = day + '/' + month + '/' + year;
                     }
              }
        </script>

But I am getting the wrong value. Please someone, where I went wrong.

Can't make a new Date() with dd/mm/yyyy . Try this:

function datechange() {
    var d      = document.getElementById('days').value;
    var from   = document.getElementById('date1').value.split('/');
    var myDate = new Date(from[2], from[1] - 1, from[0]);
    myDate.setDate(myDate.getDate() + parseInt(d));
    document.getElementById('date2').value = myDate.getDate() + '/' + (myDate.getMonth() + 1) + '/' + myDate.getFullYear();
}

Working demo: http://jsfiddle.net/j5sqg/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM