Reputation: 269
I'm developing an asp.net mvc application, in which I'm trying to get a Date value using a datepicker, I've been struggling a lot to find an appropriate solution but no success, what I want it to accept: dd/mm/yyyy but after picking the date, the datepicker take always this format: mm/dd/yyyy
Screenshot
The problem that the ModelState will be not valid the day's value is bigger als 12 (debugger read that as month).
View Code :
<input id="datepicker" name="Date" type="text" value="dd/mm/yyyy" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'dd/mm/yyyy';}" required="">
<script>
$(function ()
{
$("#datepicker").datepicker({ minDate: 0 });
});
</script>
Model code :
[Required]
public DateTime Date { get; set; }
Upvotes: 1
Views: 3539
Reputation: 205
You can sample use
$('#datepicker').datepicker({ dateFormat: 'dd/mm/yyyy' });
Upvotes: 0
Reputation: 1
$('#datepicker').datepicker({ dateFormat: 'dd/mm/yy' })
Instead if you want to read the value :
var date = $('#datepicker').datepicker({ dateFormat: 'dd/mm/yy' }).val();
Good luck!
Upvotes: -1
Reputation: 4475
You can customize dateFormat for jquery UI datepicker.
$("#datepicker").datepicker({ minDate: 0, dateFormat: 'dd/mm/yy' });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
<input type="text" id="datepicker"/>
Change your datepicker line to this.
$("#datepicker").datepicker({ minDate: 0, dateFormat: 'dd/mm/yy' });
Upvotes: 0
Reputation: 5962
datepicker provide option to give date format.
$('#datepicker').datepicker({ format: 'dd/mm/yyyy' });
If required then try to change current culture from Global.asax file
protected void Application_BeginRequest(Object sender, EventArgs e)
{
CultureInfo newCulture = (CultureInfo) System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
newCulture.DateTimeFormat.ShortDatePattern = "dd/MMM/yyyy";
newCulture.DateTimeFormat.DateSeparator = "/";
Thread.CurrentThread.CurrentCulture = newCulture;
}
Upvotes: 2
Reputation: 2748
You can add simple option format
:
$("#datepicker").datepicker({ minDate: 0, format:"dd/mm/yyyy"});
Upvotes: 0