9

I have a gridview on my form. I am binding it with same data. There is a template field, which I mentioned below.

<asp:TemplateField HeaderText="To">
    <itemtemplate>
    <asp:Label ID="Label2" runat="server" 
         Text='<%# Convert.ToDateTime(Eval("Leave_To")).ToString("dd/MM/yyyy") %>'></asp:Label>
    </itemtemplate>
    <headerstyle horizontalalign="Left" />
    <itemstyle horizontalalign="Left" />
</asp:TemplateField>

My issue is that when I run this form on Mozilla, Opera, Chrome etc. it shows the date format (with oblique) dd/mm/yyyy, but when I run it with ie 10 it shows format (with hyphen) dd-mm-yyyy. why?

Can anyone help on this ?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Rahul Sutar
  • 260
  • 9
  • 36
  • 4
    I don't think string representation of a `DateTime` might change based on a browser. Since you didn't provider any `IFormatProvider`, it always use your `CurrentCulture` settings. If this culture settings are the same, then these formats should be the same as well. Are you _really_ sure you running this code under the same culture settings? – Soner Gönül Jan 15 '15 at 07:22
  • @SonerGönül: Agree with you on this, then why it only changes in IE 10 ? – Rahul Sutar Jan 15 '15 at 07:23
  • 3
    Well I suggest you diagnose this by logging `CultureInfo.CurrentCulture` (or include it in the output in a test page). – Jon Skeet Jan 15 '15 at 07:26
  • 1
    @JonSkeet: Yes sure will include it and check. By the way I am using the below code to convert string into datetime. `Date_To; DateTime.TryParseExact(lbl1.Text, "dd/MM/yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out Date_From);` – Rahul Sutar Jan 15 '15 at 07:29
  • See this answer: http://stackoverflow.com/a/8099314/495455, where the ISO 8601 standard is mentioned - *perhaps thats why* – Jeremy Thompson Jan 15 '15 at 08:26
  • @JeremyThompson: I think yes you are right..!! – Rahul Sutar Jan 15 '15 at 08:38
  • 2
    In terms of a fix, have you tried specifically setting the date culture for the website (http://msdn.microsoft.com/en-us/library/bz9tc508%28v=vs.140%29.aspx) rather than using the default culture? – Octopoid Jan 19 '15 at 13:57
  • Have you checked the preferred language for each browser? If one is set to en-us and one to, say en-uk it might explain the difference - it's quite possible the site switches the current culture based on this (you and check as per JonSkeet's advice). If you are parsing the dates on the way in it could be parsing as US or UK depending on the culture. – Keith Paul Barrow Jan 19 '15 at 17:21

3 Answers3

3

I can't test it right now, but I guess the IE is sending a "Accept-Language" header in the request, that is different of your current culture. Thus, it is overriding the current culture of the application (Culture of the WebServer/IIS)...

Try to set this on your Web.config:

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

Set your desired culture inside the principal key. (But the culture "en-GB" already provides the desired date format).

Hope I helped...

Vitox
  • 3,852
  • 29
  • 30
0

If you want to override the culture so that the format is always 'dd/MM/yyyy' then you can use the following.

Convert.ToDateTime(Eval("Leave_To")).ToString("dd/MM/yyyy", DateTimeFormatInfo.InvariantInfo)
Josh
  • 1,724
  • 13
  • 15
0

It looks like you want to force the delimiters to be a specific character (i.e., a '/' character) instead of what the default culture provides. This is easily accomplished by hard-coding the delimiters as literals in your date formatting string, to wit:

foo.ToString("dd'/'MM'/'yyyy")

Note the addition of quote marks surrounding the '/' delimiter characters.

David R Tribble
  • 11,918
  • 5
  • 42
  • 52