System.Globalization.DateTimeFormatInfo.InvariantInfo

来源:互联网 发布:闽南歌心事谁人知简谱 编辑:程序博客网 时间:2024/06/17 05:37

如果在不改变当前线程的Culture的前提下,如果要用ToString("dd-MM-yyyy")来自定义格式.最好加上System.Globalization.DateTimeFormatInfo.InvariantInfo.如果不加上,可能还受当前Culture的影响

即string aa = DateTime.Now.ToString("dd-MM-yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo);


DateTime格式是跟当前Thread的Cultrue设置,Thread类提供了CurrentCulture和CurrentUICulture,CurrentCulture用于设置格式化和排序的文化,CurrentUICulture用于设置用户界面语言的文化,如果不设置,CurrentCulture默认跟系统的区域设置,CurrentUICulture默认跟操作系统语言,如果按照了多语言用户界面,也跟系统区域设置.如果要定义全局Cultrue的话,可以在Web.Config里面定义.我们这次测试,就直接在页面上设置线程的Cultrue了.


System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("es-ES");  
System.Threading.Thread.CurrentThread.CurrentCulture = ci;  
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;  
this.lblDate.Text += string.Format("西班牙:<br/> {0}", DateTime.Now.ToString());  
this.lblDate.Text += string.Format("<br/ > {0}", DateTime.Now.ToShortDateString());  
this.lblDate.Text += string.Format("<br /> {0} <br /><br />", DateTime.Now.ToLongDateString());  
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("es-ES");
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
this.lblDate.Text += string.Format("西班牙:<br/> {0}", DateTime.Now.ToString());
this.lblDate.Text += string.Format("<br/ > {0}", DateTime.Now.ToShortDateString());
this.lblDate.Text += string.Format("<br /> {0} <br /><br />", DateTime.Now.ToLongDateString());
 
经过上述的设置,ToString的输出就会跟西班牙Culture的格式输出.结果如下:view plaincopy to clipboardprint?
西班牙:  
12/12/2008 11:30:47  
12/12/2008  
viernes, 12 de diciembre de 2008  


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/YANGZHONGBAO411/archive/2009/10/28/4738606.aspx

 

PS:

// 获取本地化时间 星期几;
string week = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek);

返回“星期五”

 

将2009-6-19表示成2009年6月19日
要先引用 using System.Globalization;命名空间
date.ToString("yyyy年M月d日", DateTimeFormatInfo.InvariantInfo);

或string aa = date.ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern);