0

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; }
newbie
  • 273
  • 2
  • 4
  • 13
  • use `mm` instead of `MM` and `yy` instead of `yyyy`. have a look here as well: https://jqueryui.com/resources/demos/datepicker/date-formats.html – Jameson the dog Dec 11 '16 at 15:29
  • @Jamesonthedog I updated my question. – newbie Dec 11 '16 at 15:35
  • I've updated my answer then :) – Jameson the dog Dec 11 '16 at 15:39
  • Also update `DataFormatString = {0:dd/MM/yyyy}` to `DataFormatString = {0:dd/mm/yyyy}` – mmushtaq Dec 11 '16 at 16:35
  • You can try changing the server machine date time to desired format. Else you can opt to changing your DB column into string – Rajshekar Reddy Dec 11 '16 at 17:24
  • You attributes are only applicable when using the `@Html.EditorFor()` and `@Hml.DisplayFor()` methods and do not apply when using a jquery datepicker. If the error is client side, its because you have not reconfigured the `$.validator` (refer [this answer](http://stackoverflow.com/questions/27285458/jquery-ui-datepicker-and-mvc-view-model-type-datetime/27286969#27286969)) and if its server side, then its because you server does not accept dates in `dd/MM/yyyy` format –  Dec 11 '16 at 21:40
  • And you have 2 correct answers to your original question! If you then have another question, then ask another question. –  Dec 12 '16 at 06:22

2 Answers2

1

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

deChristo
  • 1,860
  • 2
  • 17
  • 29
  • Take a look at: http://stackoverflow.com/questions/6906725/unobtrusive-validation-in-chrome-wont-validate-with-dd-mm-yyyy – deChristo Dec 11 '16 at 15:38
1

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

Jameson the dog
  • 1,796
  • 1
  • 11
  • 12