0

I am trying to change the format of my DataFormatString from dd/MM/yyyy to MM/yy. The reason is because I want to users to select the dates for their credit card expiry date. I am splitting the HTML into two parts: model and view.

Order.cs

[Display(Name = "Expiration Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime Experation { get; set; }

Address and Payment.cshtml

<div class="form-group">
    @Html.LabelFor(model => model.Experation, htmlAttributes: new { @class = "col-lg-2 control-label" })
    <div class="col-lg-10">
        @Html.EditorFor(model => model.Experation)
        @Html.ValidationMessageFor(model => model.Experation, "", new { @class = "text-danger" })
    </div>

The above code was my attempt to convert it into MM/yy in my model. However, the result is still dd/MM/yyyy. Is there an error here that I do not know?

Brandon
  • 87
  • 1
  • 1
  • 9
  • Possible duplicate of [Display a formatted date in an EditorFor()](http://stackoverflow.com/questions/15943797/display-a-formatted-date-in-an-editorfor) – NibblyPig Feb 01 '17 at 17:31
  • Remove `[DataType(DataType.Date)]` which adds `type-"date"` and will generate a browsers HTML5 datepicker if supported (but its only supported in Chrome and Edge so you must be using one of those). –  Feb 02 '17 at 05:28
  • But your format string makes no sense. The property is typeof `DateTime` which must include a day component, and when you submit, `ModelState` will be invalid and the value of `Experation` will be `01/01/0001` (and if you have enabled client side validation, you would not even be able to submit the form) –  Feb 02 '17 at 05:37

1 Answers1

0

I don't think any of the built-in DataType are going to a handle scenario like this where you want only a month and a year. In a sense, it's not really a date but a "month + year" combination. I would just handle it as a String and use a Regex to validate it, then on the server perform further parsing and validation.

Note that the below Regex is definitely not foolproof but just meant to catch obvious input errors (e.g. YY/MM instead of MM/YY). You could make it more strict with something like (01|02|03|04|05|06|07|08|09|10|11|12)\/(20|21|22|23|24|25|26|27|28|29|30|31|32|33) since the range of options is limited.

[Display(Name = "Expiration Date")]
[RegularExpression("(0|1)[0-9]\/2[0-9]", ErrorMessage = "Enter expiration date using MM/YY format")]
public String Expiration { get; set; }

An alternative approach would be to split the field into two numeric fields ExpirationMonth and ExpirationYear. Again, with some clever Range magic you could constrain month to always be within 1 and 12 and year between 20 and 42.

Martin Devillers
  • 17,293
  • 5
  • 46
  • 88