Question

I have an asp.net 4.0 Formview bound to a SqlDataSource, hosted on IIS7, and I'm displaying a date for editing like this:

<input id="OrderDateTextBox" type="text" value='<%# Eval("OrderDate", "{0:d}") %>' />

This generally works as expected, but occasionally the date format displays d/m/y instead of m/d/y. When this happens, a page refresh will show the correct date format.

So far I've tried setting the default culture in web.config like this:

<globalization enableClientBasedCulture="false" culture="en-US" uiCulture="en-US" />

I've also tried setting the culture on initialization:

protected override void InitializeCulture()
{
    UICulture = "en-US";
    Culture = "en-US";
    Thread.CurrentThread.CurrentCulture =
        CultureInfo.CreateSpecificCulture("en-US");
    Thread.CurrentThread.CurrentUICulture =
        new CultureInfo("en-US");

    base.InitializeCulture();
}

Neither of those methods seem to have any effect. However, if I pull in the value by using the following syntax the date format is correct all the time:

<%# DateTime.Parse(Eval("OrderDate").ToString()).ToString("d", CultureInfo.CreateSpecificCulture("en-US")) %>

But that shouldn't be necessary. Thanks for any help.

Was it helpful?

Solution

Instead of using {0:d} and depending on the culture to format the date, you can try forcing the date into the formatting you wish. Example:

<%# Eval("OrderDate", "{0:MM/dd/yyyy}") %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top