1

I had one logic issue while date .

    Date:<input id="txtDate" type="text" />
<input type="button" onclick="getdate()" value="Fill Follow Date" />
Follow Date:<input id="follow_Date" type="text" />

Add Script

    $(document).ready(function () {
    $('#txtDate').datepicker();
    $('#follow_Date').datepicker();
});

function getdate() {
    var tt = document.getElementById('txtDate').value;

    var date = new Date(tt);
    var newdate = new Date(date);

    newdate.setDate(newdate.getDate());

    var dd = newdate.getDate()+1;
    var mm = newdate.getMonth()+1;
    var y = newdate.getFullYear();

    var someFormattedDate = dd + '-' + mm + '-' + y;
    document.getElementById('follow_Date').value = someFormattedDate;
}

For today "31-03-2016" the follow date show "32-03-2016". But its show the 01-04-2016. Do you have any other logic for to over come this error. We had the options like "makemytrips" hotel page. makemytrip.com/hotels here you can see the number of nigths. same concept i used for my site

vignesh raj kumar
  • 181
  • 1
  • 1
  • 9
  • Just `newdate.setDate(newdate.getDate() + 1);` Do not add it later.. – Rayon Mar 31 '16 at 10:12
  • Don't use the Date constructor to parse strings, it's extremely unreliable. Manually parse strings, use a library or a [*simple function*](https://stackoverflow.com/questions/33908299/javascript-parse-a-string-to-date-as-local-time-zone/33909265#33909265). – RobG Mar 31 '16 at 14:48

3 Answers3

1

You are not doing a date addition, instead a numerical addition. getDate() returns a string number value of 31 to which if you add 1 you will get 32, what is wrong in that, then you are doing a string concatenating which is causing the problem.

Instead you can use the datepicker, and Date apis

$(document).ready(function() {
  var $date = $('#txtDate').datepicker();
  var $follow = $('#follow_Date').datepicker();
  $('#fill-follow').click(function() {
    var date = $date.datepicker('getDate');
    if (date) {
      date.setDate(date.getDate() + 1);
      //date.setMonth(date.getMonth() + 1);
      $follow.datepicker('setDate', date)
    }
  });
});
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.css" rel="stylesheet" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>
Date:
<input id="txtDate" type="text" />
<input type="button" id="fill-follow" value="Fill Follow Date" />Follow Date:
<input id="follow_Date" type="text" />
RobG
  • 142,382
  • 31
  • 172
  • 209
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • Ya rayon. We had the options like "makemytrips" hotel page. http://www.makemytrip.com/hotels here you can see the number of nigths. same concept i used for my site – vignesh raj kumar Mar 31 '16 at 10:11
  • Wow, 3 libraries for an extremely simple task. The code throws "$ is not defined" for me in Chrome. – RobG Mar 31 '16 at 14:51
  • @RobG If you look at the question, OP is already using jQuery UI... so why not use those apis to do the task – Arun P Johny Mar 31 '16 at 17:54
0

You need to add 1 to the getDate() result, then call setDate() on it:

function getdate() {
    var tt = document.getElementById('txtDate').value;

    var date = new Date(tt);
    var newdate = new Date(date);

    newdate.setDate(newdate.getDate() + 1);

    var dd = newdate.getDate();
    var mm = newdate.getMonth() + 1;
    var y = newdate.getFullYear();

    var someFormattedDate = dd + '-' + mm + '-' + y;
    document.getElementById('follow_Date').value = someFormattedDate;
}

Also, reading your code, I wonder why you create a date twice :

    var date = new Date(tt);
    var newdate = new Date(date);

Couldn't you just combine these into one line ?

    var newdate = new Date(tt);
Sam Bauwens
  • 1,317
  • 11
  • 18
0

Use your logic like below

var someDate = new Date();
var numberOfDaysToAdd = 6;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);

To format the date:

var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();

var someFormattedDate = dd + '/'+ mm + '/'+ y;
Rahul R G
  • 113
  • 8