2

I have a small problem setting the correct dates on my datepickers. Somehow the date shown in the textbox are not the correct one in the datepicker.

Code textboxes:

<div class="small-12 medium-6 large-3 columns">
    @Html.Label("Departure Date:", new { @class = "label-travel" })
    @Html.TextBoxFor(m => m.DepartureDate, new { Value = Model.DepartureDate.ToShortDateString(), @class = "textbox", @id = "departureDate" })
</div>

<div class="small-12 medium-6 large-3 columns">
   @Html.Label("Return Date:", new { @class = "label-travel" })
   @Html.TextBoxFor(m => m.ReturnDate, new { Value = Model.ReturnDate.ToShortDateString(), @class = "textbox", @id = "returnDate" }) 
</div>

var currentDay = ??
var currentDayPlusOne = ??

$("#departureDate").datepicker({
    dateformat: "yy-dd-mm"
});

$("#returnDate").datepicker({
    dateformat: "yy-dd-mm"
});

What should I write in order to set the current days in my datepicker, so the textbox has the date 2015-26-15 and the other day is 2015-27-15?

Hope someone can show me.

Mandersen
  • 805
  • 3
  • 14
  • 22
  • Do you want something similar to http://stackoverflow.com/questions/14591634/jquery-datepicker-set-date-to-tomorrows-date and related fidller http://jsfiddle.net/JSFXP/1/ – calinaadi Nov 26 '15 at 10:21
  • 2
    What kind of date is `2015-26-15`? Is there a calendar with 15 or 27 months? – Salman A Nov 26 '15 at 10:36
  • Duplicate of http://stackoverflow.com/questions/233553/how-do-i-pre-populate-a-jquery-datepicker-textbox-with-todays-date – Salman A Nov 26 '15 at 10:38

2 Answers2

2

Check this snippet:

Here, setDate sets today date & dateFormat defines which format you want to show.

var currentDate = new Date();  
var tomrw = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
$("#departureDate").datepicker({ dateFormat: "yy-dd-mm"}).datepicker("setDate", new Date());
$("#returnDate").datepicker({ dateFormat: "yy-dd-mm"}).datepicker("setDate", tomrw);
<link href="https://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
Departure date:
<input type="text" id="departureDate" />
Return date:
<input type="text" id="returnDate" />

You could also use setDate : 1 for setting tomorrow's date. As the value 1 means that it's one day from current date. If you want to the departure date to 10 days from the current date you can use setDate : 10.

Lucky
  • 16,787
  • 19
  • 117
  • 151
  • Works.. but is it right to call datepicker 2 times? – Mandersen Nov 26 '15 at 11:45
  • @Mandersen Of course, you can call `datepicker()` as many times on different elements. Look at the [Date Range](https://jqueryui.com/datepicker/#date-range) sample on jquery datepicker example which uses `from` and `to` fields as datepickers. – Lucky Nov 26 '15 at 11:47
0

You can do like this :

<script>
                $("#departureDate").datepicker().datepicker("setDate", new Date());

            $("#returnDate").datepicker().datepicker("setDate", new Date());
</script>
Lalji Nakum
  • 380
  • 1
  • 14
  • The second datepicker does the same thing as the first one and the OP wants the returnDate set to one day apart from today's date. so your code won't work. ;( – Lucky Nov 26 '15 at 12:16
  • @Lucky I know but there is no any specification regarding this. so i have not added code for that. – Lalji Nakum Nov 27 '15 at 15:06