C# DateTime

C# DateTime

Asp.net DateTime formatting Strings issues are very common in day to day development of applications.Dot net made life simple. There are two ways you can set the DateTime formats specific to culture.Let's see how to deal with DateTime in C#.net and Vb.net.After reading this post you will get an idea about how to deal with DateTime issues with website and multi culture.

c# Date Format

DateTime in [C#]

The culture and UICulture properties can be set using internet standard string like.
1. en-US where en stands for English and US stands for United States.
2. en-GB where en stands for English and GB stands for Great Britain.
3. es-MX where es stands for Spanish and MX stands for Mexican.

  1. CurrentCulture.
  2. CurrentUICulture.

DateTime default culture in (.net)

The default culture of .net is en-US, if operating system uses en-US as a language. In US the date time format is MM-DD-YYYY. This is obvious that when you install windows operating system it shows dates in US date time formats. The term CurrentCulture is specific to entire application and available for its life time.

All threads generated by those applications will use Current Culture Information unless and until you change it to use UI specific culture information.

DateTime Example.

For example. Suppose you are Maharashtrian and you speak Marathi language since childhood, you went to another part of region/country where their culture is English. What you will do? Answer is you will start speaking in English if you know it. Same way some part of application can be rendered in many languages with the help of CurrentUICulture.

In previous example Marathi is your mother tongue the base language and others are just culture specific. Same way we can keep the main culture as CurrentCulture and sub culture as UICulture. In Asp.net you can set UI Culture information like this.

In Web.Config or App.Config
 


You can also change the Culture information by referring below codes.We need to use below namespaces.

C#.net DateTime culture

1.
using  System.Threading;
using  System.Globalization;
protected void Button1_Click(object sender, EventArgs e)
{        
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
FromDate.Text = DateTime.Now.ToShortDateString();
}
OutPut:
The output Date is “8/4/2011” which is in US MM/DD/YYYY format
2.
protected void Button1_Click(object sender, EventArgs e)    
{        
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");           
FromDate.Text = DateTime.Now.ToString();      
} 
OutPut:
Date and Time is “04/08/2011 19:52:38” which is in UK date and time format DD/ MM /YYYY.
protected void Button1_Click(object sender, EventArgs e)    
{        
Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");         
FromDate.Text = DateTime.Now.ToString();     
} 

Add culture information to (Asp.net) Page

You can also set Page specific culture information by adding UICulture and Culture attributes in Asp.net page as shown in below Example.

C# Format
By adding UICulture attribute inside page 
<%@ Page UICulture="en" Culture="en-US" %> 
Remember:
  1. If you want to set default DateTime of web Application in specified language, use CurrentCulture.
  2. The page specific date and time can be specified using CurrentUICulture. You can also see the complete list of available language support at below link. http://msdn.microsoft.com/en-us/goglobal/bb896001.
C# DateTime Format
Code	 Language	 Code	 Language
af	Afrikaans 	sq	Albanian 
ar-sa	Arabic (Saudi Arabia) 	ar-iq	Arabic (Iraq) 
ar-eg	Arabic (Egypt) 	ar-ly	Arabic (Libya) 
ar-dz	Arabic (Algeria) 	ar-ma	Arabic (Morocco) 
ar-tn	Arabic (Tunisia) 	ar-om	Arabic (Oman) 
ar-ye	Arabic (Yemen) 	ar-sy	Arabic (Syria) 
ar-jo	Arabic (Jordan) 	ar-lb	Arabic (Lebanon) 
ar-kw	Arabic (Kuwait) 	ar-ae	Arabic (U.A.E.) 
ar-bh	Arabic (Bahrain) 	ar-qa	Arabic (Qatar) 
eu	Basque 	bg	Bulgarian 
be	Belarusian 	ca	Catalan 
zh-tw	Chinese (Taiwan) 	zh-cn	Chinese (PRC) 
zh-hk	Chinese (Hong Kong SAR) 	zh-sg	Chinese (Singapore) 
hr	Croatian 	cs	Czech 
da	Danish 	nl	Dutch (Standard) 
nl-be	Dutch (Belgium) 	en	English 
en-us	English (United States) 	en-gb	English (United Kingdom) 
en-au	English (Australia) 	en-ca	English (Canada) 
en-nz	English (New Zealand) 	en-ie	English (Ireland) 
en-za	English (South Africa) 	en-jm	English (Jamaica) 
en	English (Caribbean) 	en-bz	English (Belize) 
en-tt	English (Trinidad) 	et	Estonian 
fo	Faeroese 	fa	Farsi 
fi	Finnish 	fr	French (Standard) 
fr-be	French (Belgium) 	fr-ca	French (Canada) 
fr-ch	French (Switzerland) 	fr-lu	French (Luxembourg) 
gd	Gaelic (Scotland) 	ga	Irish 
de	German (Standard) 	de-ch	German (Switzerland) 
de-at	German (Austria) 	de-lu	German (Luxembourg) 
de-li	German (Liechtenstein) 	el	Greek 
he	Hebrew 	hi	Hindi 
hu	Hungarian 	is	Icelandic 
id	Indonesian 	it	Italian (Standard) 
it-ch	Italian (Switzerland) 	ja	Japanese 
ko	Korean 	ko	Korean (Johab) 
lv	Latvian 	lt	Lithuanian 
mk	Macedonian (FYROM)	ms	Malaysian 
mt	Maltese 	no	Norwegian (Bokmal) 
no	Norwegian (Nynorsk) 	pl	Polish 
pt-br	Portuguese (Brazil) 	pt	Portuguese (Portugal) 
rm	Rhaeto-Romanic 	ro	Romanian 
ro-mo	Romanian (Republic of Moldova) 	ru	Russian 
ru-mo	Russian (Republic of Moldova) 	sz	Sami (Lappish) 
sr	Serbian (Cyrillic) 	sr	Serbian (Latin) 
sk	Slovak 	sl	Slovenian 
sb	Sorbian 	es	Spanish (Spain) 
es-mx	Spanish (Mexico) 	es-gt	Spanish (Guatemala) 
es-cr	Spanish (Costa Rica) 	es-pa	Spanish (Panama) 
es-do	Spanish (Dominican Republic) 	es-ve	Spanish (Venezuela) 
es-co	Spanish (Colombia) 	es-pe	Spanish (Peru) 
es-ar	Spanish (Argentina) 	es-ec	Spanish (Ecuador) 
es-cl	Spanish (Chile) 	es-uy	Spanish (Uruguay) 
es-py	Spanish (Paraguay) 	es-bo	Spanish (Bolivia) 
es-sv	Spanish (El Salvador) 	es-hn	Spanish (Honduras) 
es-ni	Spanish (Nicaragua) 	es-pr	Spanish (Puerto Rico) 
sx	Sutu 	sv	Swedish 
sv-fi	Swedish (Finland) 	th	Thai 
ts	Tsonga 	tn	Tswana 
tr	Turkish 	uk	Ukrainian 
ur	Urdu 	ve	Venda 
vi	Vietnamese 	xh	Xhosa 
ji	Yiddish 	zu	Zulu 

Other Examples of (DateTime)

How to format Time in C#.net and VB.net program.
DateTime Time Format string {HH:mm:ss}.
C#.net date time Culture.
C#.net DateTime Month.
string was not recognized as a valid date time
Get Today
DateTime in English UK format
Get Day Of year
DateTime.Today vs DateTime.Now
DateTime Parse Exact method.
Parse Exact DateTime

Tags:

C# DateTime DifferenceC# Parse method of DateTimeDateTime.Today Vs DateTime.NowC# DateTimeC# DateTime ToString()IFormatProvider DateTime C#.netDateTimeTryParse with IFormatProvider culture information using c#C# Convert String To DateTimeDateTime in C# current Date and TimeC# Datetime MonthC#.net Get Yesterday.

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.