Reputation: 3945
MVC- using a datepicker, which when initially loads I would like to display todays date by default, I have been trying this but for today it is returning 03/9/2013, i need it to say dd/mm/yyyy - 25/09/2013.
Then on the post back it will store whatever date the user has chosen, instead it jumps back to this date, so iv used a bool in the get isInitial, and set it to true, then in the post function set it to false, and in VIEW want to use code to display todays date, only IF(isInitial)...
is this the correct way about this? Also any help on resolving todays date would be great thanks
<div class="editor-label">@Html.TextBoxFor(model => model.SelectedDate, new { @class = "jquery_datepicker", @Value = Model.SelectedDate.HasValue ? Model.SelectedDate.Value.ToString("dd/MM/yyyy") : string.Empty })</div>
@using (Script.Foot())
{
<script type="text/javascript" language="javascript">
$(function () {
var dates = $("#SelectedDate").datepicker().val(dates);
});
$(function () {
var myDate = new Date();
var todayDate = myDate.getDay() + '/' + (myDate.getMonth() + 1) + '/' + myDate.getFullYear();
$("#SelectedDate").datepicker().val(todayDate);
});
</script>
}
Upvotes: 0
Views: 1578
Reputation: 2087
Did you try this
$("#SelectedDate").datepicker({
dateFormat: 'dd/mm/yyyy'
}).val(todayDate);
Upvotes: 1