System.Globalization.DateTimeFormatInfo.InvariantInfo用法
2009-10-28 15:32
288 查看
如果在不改变当前线程的Culture的前提下,如果要用ToString("dd-MM-yyyy")来自定义格式.最好加上System.Globalization.DateTimeFormatInfo.InvariantInfo.如果不加上,可能还受当前Culture的影响
DateTime格式是跟当前Thread的Cultrue设置,Thread类提供了CurrentCulture和CurrentUICulture,CurrentCulture用于设置格式化和排序的文化,CurrentUICulture用于设置用户界面语言的文化,如果不设置,CurrentCulture默认跟系统的区域设置,CurrentUICulture默认跟操作系统语言,如果按照了多语言用户界面,也跟系统区域设置.如果要定义全局Cultrue的话,可以在Web.Config里面定义.我们这次测试,就直接在页面上设置线程的Cultrue了.
经过上述的设置,ToString的输出就会跟西班牙Culture的格式输出.结果如下:
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());
经过上述的设置,ToString的输出就会跟西班牙Culture的格式输出.结果如下:
西班牙: 12/12/2008 11:30:47 12/12/2008 viernes, 12 de diciembre de 2008
相关文章推荐
- C#中System.Globalization.DateTimeFormatInfo.InvariantInfo怎么用
- System.Globalization.DateTimeFormatInfo.InvariantInfo
- System.Globalization.CultureInfo.InvariantCulture失效
- System.Globalization.CultureInfo.InvariantCulture在Silverlight程序中的作用
- System.Globalization.NumberFormatInfo 的 10个方法、30个属性和1个构造函数
- System.Globalization.NumberFormatInfo 的 30个属性、1个构造函数和10个方法
- DateTimeFormatInfo in DateTime.Now.ToString
- System.DateTime.Now.ToString()日常用法
- System.DateTime.Now.ToString()的一些用法 [转]
- ALV report listing all the infotypes that are customized in a SAP system with the structure name
- info.plist utility error:The data couldn’t be read because it isn’t in the correct format
- System.in.read()的用法
- System.in.read()的用法
- How to format datetime & date in Sql Server 2005
- [原] XAF How to see and edit the time part in the DatePropertyEditor for the System.DateTime property
- CultureInfo.InvariantCulture 作用
- 用DateTimeFormatInfo格式化日期时间
- How To Get System Info In Linux
- 学习笔记之system.in的用法
- .NET(C#):ToString(CultureInfo.InvariantCulture)