-1

I have a implemented a datepicker, on the selected date, should +2 to the date. and return the both dates that is before adding +2 and after adding +2.

for eg

from the datepicker, will get date like thisWed Jan 08 2020 15:53:01 GMT+0800 (Philippine Standard Time)

I would like to know how to do in Javascript, if the date is Jan 31 2020 15:53:01 GMT+0800, the should be

31-01-2020,
01-02-2020

also if the date is 31 Dec 2019 should be 31-12-2019, 01-01-2020 Expected Output:

08-01-2020,
09-01-2020
var result = function getAllDates("Wed Jan 08 2020 15:53:01 GMT+0800 (Philippine Standard Time)");

functions getAllDates(datestr){
   var formatDate = dateStr.toLocaleDateString("en-GB").replace(/\//g,"-");
   var splitdate = dateStr.getDate() + 2;
   var splitmonth = dateStr.getMonth();
var splityear = dateStr.getYear();
return splitdate+"-"+splitmonth+"-"+splityear
}



miyavv
  • 763
  • 3
  • 12
  • 30
  • I cant find any logic in your examples, i guess it requires adjustment – Dmitry Reutov Jan 08 '20 at 08:17
  • What is the logic behind getting your Expected Output? – Mayank Patel Jan 08 '20 at 08:54
  • @MayankPatel thanks for reply, instead of adding days to todays date, i need to add 2days to selected date, – miyavv Jan 08 '20 at 08:55
  • @MayankPatel https://stackoverflow.com/questions/9989382/how-can-i-add-1-day-to-current-date instead i want to do same in selected date + 2day – miyavv Jan 08 '20 at 08:57
  • Means for ex. in jquery datepicker, I have selected "2020-01-08", you need 2 dates in output which are "2020-01-08" and "2020-01-10"? – Mayank Patel Jan 08 '20 at 08:59
  • @MayankPatel yes but i need to get "2020-01-08", "2020-01-09" – miyavv Jan 08 '20 at 09:01
  • You said "+2 to the date" then how would you get "2020-01-09" in result? – Mayank Patel Jan 08 '20 at 09:02
  • @MayankPatel apologies, yes should be "2020-01-08", "2020-01-10", – miyavv Jan 08 '20 at 09:05
  • Please update (edit) the question. *all* your examples are +1day, but in the question and comments you want +2 days. – freedomn-m Jan 08 '20 at 09:15
  • If you wanted to add 2 to the selected date, kindly refer to my answer. If you want to add 1, just add "86400000" which is nothing but 1 day in milliseconds. – Mayank Patel Jan 08 '20 at 09:20
  • There's **so many issues** with your code as provided, it's not really worth the time picking them all out. Sorry. To add days, use `dateObj.setDate(dateObj.getDate()+2)` but you mix up strings and objs too much. – freedomn-m Jan 08 '20 at 09:23
  • Does this answer your question? [Add days to JavaScript Date](https://stackoverflow.com/questions/563406/add-days-to-javascript-date) – freedomn-m Jan 08 '20 at 09:23

1 Answers1

0

You can simply achieve it like this:

$(function(){
        $(".datepicker").datepicker({
                dateFormat: 'yy-mm-dd',
                autoclose: true,
                todayHighlight: true,
            });
    });
function getResult(){
  var selectedDate = new Date($(".datepicker").val());
  var nextDate = new Date(selectedDate.getTime() + 172800000); // + 2 day in ms
  console.log(selectedDate);
  console.log(nextDate);
  $("#selected").html(selectedDate);
  $("#selected_plus_2").html(nextDate);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<input type="text" class="datepicker" />
<input type="button" onclick="getResult()" value="Get Result" />
<br />
Selected: <span id="selected"></span><br />
Selected + 2: <span id="selected_plus_2"></span>

EDIT: Another way to do it is:

function getResult(){
  var selectedDate = new Date($(".datepicker").val());
  var nextDate = new Date(selectedDate);
  nextDate.setDate(selectedDate.getDate()+2);
  nextDate.toLocaleDateString();
  console.log(selectedDate);
  console.log(nextDate);
}
Mayank Patel
  • 1,563
  • 1
  • 14
  • 19