Posts

Showing posts from July, 2011

C# format date times for different locals (localization and globalization), DateTimeFormatInfo

Image
Working on a project recently I found we were presenting date/time data to a variety of users in various countries. Each country uses it's own date/time formatting. Here in the US it's often MM/dd/yyyy, however in Europe it's often dd/MM/yyyy and in Asia they don't even have the slashes they use special characters that designate year, month, and day. At first we were going to come up with localized versions of all the possible date/time combinations you can display (June 6th, 1977, 6/25/77, etc) but Microsoft has an even better solution already in place. static void Main( string[] args ) { DateTime dt = DateTime.Now; // let's create some different date times for different cultures // JAPANESE CultureInfo culture = new CultureInfo("ja-JP"); DateTimeFormatInfo formatInfo = culture.DateTimeFormat; String time = dt.ToString( formatInfo.FullDateTimePattern, culture ); MessageBox.Show( time );