newbie
newbie

Reputation: 283

jQuery datepicker dosen't work as excepted

I need my date input to be dd/MM/yyyy, but when I use this code:

$('#DateTime').datepicker({ dateFormat: 'dd/MM/yyyy' });

It shows the date like this: 28/December/20162016. Any idea?

Update: It works now with this format dd/mm/yy. But the problem is that, when I pass a date like 17/12/2016 the server says that: (it gets null)

The value '17/12/2016' is not valid for DateTime.

PS: I'm using ASP.NET MVC and my property is:

[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? DateTime { get; set; }

Upvotes: 0

Views: 66

Answers (2)

Jameson the dog
Jameson the dog

Reputation: 1806

MM is the name of the month (December) you want mm - the month's number (12).

plus - yy is enough to get the year, you wrote it twice (yyyy) so you're getting it twice: $('#DateTime').datepicker({ dateFormat: 'dd/mm/yy' });

have a look at this as well: https://jqueryui.com/resources/demos/datepicker/date-formats.html

EDIT

dd/mm/yy is not a valid date to systems (bummer, I know) I use php mostly but yy-mm-dd works on all the systems I know, so you should try $('#DateTime').datepicker({ dateFormat: 'yy-mm-dd' });

if you want it to be displayed in dd/mm/yy but posted in yy-mm-dd you'll need to use the altField and altFormat options of the date picker: http://api.jqueryui.com/datepicker/#option-altField

Upvotes: 1

deChristo
deChristo

Reputation: 1878

You need to lower case yout 'MM':

$('#DateTime').datepicker({ dateFormat: 'dd/mm/yyyy' });

According to datepicker documentation:

mm - month of year (two digit)

M - month name short

MM - month name long

Take a look at: http://api.jqueryui.com/datepicker/#option-dateFormat

Upvotes: 1

Related Questions