1

I'm referring to below link: stackoverflow.com/questions/18288675/display-datetime-value-in-dd-mm-yyyy-format-in-mvc4

Your model

  [Required(ErrorMessage = "Enter the Issued date.")]
  [DataType(DataType.Date)]
  public DateTime IssueDate { get; set; }

Razor Page

 @Html.TextBoxFor(model => model.IssueDate)
 @Html.ValidationMessageFor(model => model.IssueDate)

Jquery DatePickter

$(document).ready(function () {
    $('#IssueDate').datepicker({
        dateFormat: "dd/mm/yy",
        showStatus: true,
        showWeeks: true,
        currentText: 'Now',
        autoSize: true,
        gotoCurrent: true,
        showAnim: 'blind',
        highlightWeek: true
    });
});

Webconfig File

<system.web>
    <globalization uiCulture="en" culture="en-GB"/>
</system.web>

It works when using date format dd/MM/yyyy like 3/5/2015 but wont work when using date like 29/5/2015.

The textbox will show 29/5/2015 once we selected date from jquery datepicker. Just that when it post back to server, the model will show that the date is null (I'm using DateTime?). It will work just fine with 3/5/2015...

Any help?

EDIT:

I add below to my controller. When I postback, the function below will execute and it is showing en-GB. Then it will enter my other Action that suppose to handle the httppost, but it is showing en-US!

protected override void Initialize(System.Web.Routing.RequestContext requestContext) 
{ 
    base.Initialize(requestContext); 
    const string culture = "en-GB"; 
    CultureInfo ci =  CultureInfo.GetCultureInfo(culture);
    Thread.CurrentThread.CurrentCulture = ci;  
    Thread.CurrentThread.CurrentUICulture = ci; 
} 
Community
  • 1
  • 1
user3663854
  • 463
  • 2
  • 8
  • 21
  • Out of interest, what happens if you try 5/29/2015? – Steve May 19 '15 at 05:54
  • ^Steve, yes it can accept it... – user3663854 May 19 '15 at 06:02
  • Then you have an issue with the date culture, it's trying to parse it as an American date (mm/dd/yy) which 29/5/2015 fails. You'll need to look into your culture settings. – Steve May 19 '15 at 06:04
  • 1
    I add below to my controller. When I postback, the function below will execute and it is showing en-GB. Then it will enter my other Action that suppose to handle the httppost, but it is showing en-US! **protected override void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext); const string culture = "en-GB"; CultureInfo ci = CultureInfo.GetCultureInfo(culture); Thread.CurrentThread.CurrentCulture = ci; Thread.CurrentThread.CurrentUICulture = ci; }** – user3663854 May 19 '15 at 06:23
  • 1
    ^Steve, I checked again my code and it seem that in BeginExecuteCore, I am setting the culture to "en-US". I changed that and now my Action method is executing with correct culture and the code work. Thanks for your comment, it changed my view on the root of the problem. If you put an answer to it, I will gladly accept it... – user3663854 May 19 '15 at 06:46

2 Answers2

1

To add my comment as an answer:

To test, try giving it 5/29/2015 as the date. If this parses, then you have an issue with the culture of your application since it's trying to parse the date as an American format (mm/dd/yyyy) which 29/5/2015 fails.

Have a look in your application settings to see if en-US is being set anywhere, or if you accept the date to your controller as mm/dd/yyyy then parse it to your preferred format there.

Steve
  • 9,335
  • 10
  • 49
  • 81
0

I would start off with changing your Razor syntax from:

@Html.TextBoxFor(model => model.IssueDate)

to

@Html.EditorFor(model => model.IssueDate)

What is your browser's culture? It is as though the application is parsing the date to a different culture like en-US, which would then fail because there is no 29th month.

I think you'll also need to change your date format to dd/MM/yyyy. On MSDN there is a lot of information about this.

hbulens
  • 1,872
  • 3
  • 24
  • 45