2

I have DateTimes in model:

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

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

If I write 25.05.2016 10:00 it says that the field Start/End is not a date, but if I write 12.05.2016 10:00 it is correct. First number is matched to Day (12), second to Month (05), last to Year (2016),10 to Hour and 00 to Minutes,so why it can't let me write Day more than 12?

Cezar
  • 345
  • 6
  • 18
  • In your View how are you displaying the textboxes? `@Html.EditorFor(model => model.Start)`? – Grizzly Nov 18 '16 at 19:07
  • Are you using jQuery datepicker or any plug-in? Or are you writing in the date manually? – Grizzly Nov 18 '16 at 19:21
  • I'm writting it manualy – Cezar Nov 18 '16 at 19:52
  • 1
    If this is a client side validation error then you need to reconfigure the `$.validator` (refer [this answer](http://stackoverflow.com/questions/39677035/date-of-birth-validation-keeps-showing/39682410#39682410)). If its a server side validation error, then you need to set the culture to one that accepts `dd/MM/yyyy` format or create a custom `ModelBinder` –  Nov 18 '16 at 20:29
  • You need to give more information about when this is happening. –  Nov 18 '16 at 20:36
  • I think it's client side (because it doesn't go to HttpPost if the day number is greater than 12), but I didn't code any client side validators. If I write 12.05.2016 then 12 is correctly a Day, 05 is Month and 2016 is a Year, but validator doesn't accept day greater than 12. – Cezar Nov 18 '16 at 22:53
  • 1
    @Cezar, The reconfigure the `$.validator` to accept dates in `dd/MM/yyyy` format as per the link in my previous comment. –  Nov 18 '16 at 23:44
  • By a mistake I deleted jquery script load, that's why it worked for my, but I could even wrtite such like this "dadasdas" and it would go to POST function. – Cezar Nov 19 '16 at 15:57
  • I've found something like this and it works (the only error is that it accepts 30 and 31 of February, but I think it doesn't matter a lot) `jQuery(function ($) { $.validator.addMethod( "date", function (value, element) { var dateReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[2][0]\d{2})$|^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[2][0]\d{2}\s([0-1]\d|[2][0-3])\:[0-5]\d\:[0-5]\d)$/; return value.match(dateReg); }, "Invalid date" ); });` – Cezar Nov 19 '16 at 16:50

4 Answers4

1

Instead of doing this directly in the class, try it within the View.

@Html.EditorFor(model => model.Start, "{0:dd/MM/yyyy HH:mm}")

@Html.EditorFor(model => model.End, "{0:dd/MM/yyyy HH:mm}")

Then in your model just do:

[DataType(DataType.DateTime)]      
public DateTime Start { get; set; }

[DataType(DataType.DateTime)]
public DateTime End { get; set; }

Let me know if this helps!

Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • Doesn't work, of course first number is a day correctly, but this stupid validator doesn't let it go to post function if day is greater than 12... – Cezar Nov 18 '16 at 19:13
  • @Cezar so does the error happen when you click submit, or as soon as the focus leaves the textbox? – Grizzly Nov 18 '16 at 19:18
  • I click create and it checks if textbox value is OK, without reloading page. – Cezar Nov 18 '16 at 19:43
  • @Cezar so it goes to the `[HttpPost]` Action? And then it fails on what? Where does the exception occur? – Grizzly Nov 18 '16 at 20:22
  • It goes to HttpPost only when first number is less than 13. – Cezar Nov 18 '16 at 22:48
0

What you have here in the code below is just the display format.

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:dd/MM/yyyy HH:mm}")]    

default date time format is MM/dd/yyyy HH:mm, so actually for date time 12.05.2016 10:00, 12 is the month , 05 is the day and 2016 year.

C# applies this validation if you try to set date having other formats.

And the purpose of display format is to render the said date in dd/mm/yyyy.

  • So how can I change it to work as I want it to work? As I wrote, first number is day, not month, but it doesn't let me set day greater than 12 (I checked that by previewing my DateTime object in post controller) – Cezar Nov 18 '16 at 18:41
  • you need to do something like this – Michael Kovattil Nov 18 '16 at 18:54
0

DateTime dt=DateTime.ParseExact("24/01/2013", "dd/MM/yyyy", CultureInfo.InvariantCulture); please add the time format HH:mm if you need that information also.

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

and this in your view:

@Html.EditorFor(x => x.Start)

this should work.

Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • Doesn't work, of course first number is a day correctly, but this stupid validator doesn't let it go to post function if day is greater than 12... – Cezar Nov 18 '16 at 19:13
  • Can you post your cshtml code here. So we can have a look and suggest what went wrong. – Michael Kovattil Nov 18 '16 at 19:18
  • [cshtml](http://pastebin.com/D34ryCFU) [create view](http://pastebin.com/NrBMQquB) [controller functions](http://pastebin.com/MShQ51HK) – Cezar Nov 18 '16 at 19:47