DateTime format
-
The Date in my sql server storedin US format, that is MM/DD/YYYY. I need to display the date in Asia format, or British format, that is DD/MM/YYYY.
DateTimeFormatInfo Asian_DateTimeFormat = new CultureInfo("en-GB", false).DateTimeFormat; Console.WriteLine ("1. DateTime: 10/15/2004: " + System.DateTime.Parse ("10/15/2004", Asian_DateTimeFormat).ToShortDateString());
Above code give me an error.DateTimeFormatInfo Asian_DateTimeFormat = new CultureInfo("en-GB", false).DateTimeFormat; Console.WriteLine ("2. DateTime: 10/2/2004: " + System.DateTime.Parse ("10/2/2004", Asian_DateTimeFormat).ToShortDateString());
This code however successfully executed, and return me 2/10/2004. Wht can't the first code did the same? Any idea? Thanks! :D regards, vic -
The Date in my sql server storedin US format, that is MM/DD/YYYY. I need to display the date in Asia format, or British format, that is DD/MM/YYYY.
DateTimeFormatInfo Asian_DateTimeFormat = new CultureInfo("en-GB", false).DateTimeFormat; Console.WriteLine ("1. DateTime: 10/15/2004: " + System.DateTime.Parse ("10/15/2004", Asian_DateTimeFormat).ToShortDateString());
Above code give me an error.DateTimeFormatInfo Asian_DateTimeFormat = new CultureInfo("en-GB", false).DateTimeFormat; Console.WriteLine ("2. DateTime: 10/2/2004: " + System.DateTime.Parse ("10/2/2004", Asian_DateTimeFormat).ToShortDateString());
This code however successfully executed, and return me 2/10/2004. Wht can't the first code did the same? Any idea? Thanks! :D regards, vicTwo reasons: 1) There are only 12 months in a year. 2) The second parameter in the Parse method is the format provider to use when parsing the date. By including the Asian culture in the Parse method, you told it that the date was in the form of DD/MM/YYYY, not MM/DD/YYYY. Since your parsing a US/English format date, you have to supply the Parse method the US/English culture. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Two reasons: 1) There are only 12 months in a year. 2) The second parameter in the Parse method is the format provider to use when parsing the date. By including the Asian culture in the Parse method, you told it that the date was in the form of DD/MM/YYYY, not MM/DD/YYYY. Since your parsing a US/English format date, you have to supply the Parse method the US/English culture. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Thanks Dave. :) I am now cracking my head to solve the problem. My server default date is US format, and the sql server as well. However, when users enter data through the web, they will enter 18/11/2004 (dd/mm/yyyy), I am trying to convert it and store in sql server using 11/18/2004 (mm/dd/yyyy). I wanted to do in dynamic way, hence I think globalization method in .Net is the solution. I try to parse by US format at first, then UK format... however end up still having error. How shall I deal with this? :sigh: Thanks! regards, vic