Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get date and time formats based on Culture Info?

Tags:

c#

asp.net

What I want is.. If culture is en-US then

string dateFormat="MM/dd/yyyy";  string timeFormat="24.00 hrs"; 

If culture is en-GB then

string dateFormat="dd/mmyyyy";  string timeFormat="24.00 hrs"; 

and so on for other countries..

Now how do I get these date and time format values ? What are the standards? Like which all countries use similar date/time formats and which ones don't ?

ok I tried this :-

 DateTime myDate = new DateTime();    string us = myDate.ToString(new CultureInfo("en-US")); 

string us gets value =1/1/0001 12:00:00 AM

Now how do I extract "dd/mm/yyyy" and "24.00 hrs" out of this...in my Dateformat column in my Table... I want to store STRINGS such as dd/mm/yyyy or mm/dd/yyyy NOT dates..In my TimeFormat column in the table, the values to be stores are STRINGS too, like I need to store either "24:00hrs" or "12:00hrs"

How do I do this now ?

**using ShorTimePattern returns these values as

h:mm tt and HH:mm 

If I want to store the values in my DB exactly as "24:00hrs" and "12:00hrs", how do I use these values..h:mm tt and HH:mm which one is for 24 hr format and which for 12 hr format ?**

ok now there's another problem too...I want the information about Decimal Separator and Thousand Separator too based on the CultureInfo...whats the property for that ?

like image 524
Serenity Avatar asked Dec 04 '10 11:12

Serenity


People also ask

What formats for displaying date and time?

The dates appear as, mm/dd/yyyy in the U.S. and as, dd/mm/yyyy outside the U.S. where mm is the month, dd is the day, and yyyy is the year. The time is displayed as, hh:mm:ss AM/PM, where hh is the hour, mm is minutes, and ss is seconds.

Is there a universal date format?

No. There is no universally recognised date format. ISO 8601 Defines an international standard for date formats.

What is UTC format datetime?

Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z"). Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.


1 Answers

You can retrieve the format strings from the CultureInfo DateTimeFormat property, which is a DateTimeFormatInfo instance. This in turn has properties like ShortDatePattern and ShortTimePattern, containing the format strings:

CultureInfo us = new CultureInfo("en-US"); string shortUsDateFormatString = us.DateTimeFormat.ShortDatePattern; string shortUsTimeFormatString = us.DateTimeFormat.ShortTimePattern;  CultureInfo uk = new CultureInfo("en-GB"); string shortUkDateFormatString = uk.DateTimeFormat.ShortDatePattern; string shortUkTimeFormatString = uk.DateTimeFormat.ShortTimePattern; 

If you simply want to format the date/time using the CultureInfo, pass it in as your IFormatter when converting the DateTime to a string, using the ToString method:

string us = myDate.ToString(new CultureInfo("en-US")); string uk = myDate.ToString(new CultureInfo("en-GB")); 
like image 126
Oded Avatar answered Oct 05 '22 06:10

Oded