手机
当前位置:查字典教程网 >编程开发 >C#教程 >基于DateTime.ParseExact方法的使用详解
基于DateTime.ParseExact方法的使用详解
摘要:参数说明CultureInfo.CurrentCulture获取当前线程的区域信息中,包括DateTimeFormat日期显示格式(日期分隔...

参数说明

CultureInfo.CurrentCulture获取当前线程的区域信息中,包括DateTimeFormat 日期显示格式(日期分隔符)和 NumberFormat 货币。

试例:

1、时间中没有使用分割符的情况:

复制代码 代码如下:

string temp = "18991230" ;

DateTime dateTemp = DateTime.ParseExact(temp, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None);

2、时间中使用分割符的情况:

复制代码 代码如下:

string temp = "1899-12-30" ;

DateTime dateTemp = DateTime.ParseExact(temp, "yyyy-MM-dd", CultureInfo.CurrentCulture, DateTimeStyles.None);

DateTime dateTemp = DateTime.ParseExact(temp, "yyyy/MM/dd", CultureInfo.CurrentCulture, DateTimeStyles.None);

都正确,原因:

CultureInfo.CurrentCulture获取当前线程的CultureInfo的DateTimeFormat属性作为IFormatProvider,然后在DateTimeParse.ParseByFormat方法中,遇到format参数的/字符时,会比较输入日期字符串的当前字符是否为当前DateTimeFormatInfo的DateSeperator,如果是,则返回true,即允许转换,如果不是则返回false。当前线程的区域信息中,日期分隔符即为-,因此,转换得以成功。

像有分割符的情况最好使用下面方式:

复制代码 代码如下:

string temp = "1899-12-30" ;

DateTimeFormatInfo dtfi = new CultureInfo("zh-CN", false).DateTimeFormat;

DateTime dateTemp = DateTime.ParseExact(temp "yyyy-MM-dd", dtfi, DateTimeStyles.None) ; //使用当前分割符

【基于DateTime.ParseExact方法的使用详解】相关文章:

深入C#中使用SqlDbType.Xml类型参数的使用详解

C#组合函数的使用详解

BarCode条形码基于C# GDI+ 的实现方法详解

基于字符集、字符编码与HTTP编码解码之万象详解

VB.NET中Caching的使用方法

深入多线程之:Wait与Pulse的使用详解

基于Silverlight打印的使用详解,是否为微软的Bug问题

c#中DateTime.Now函数的使用详解

C#面向对象特征的具体实现及作用详解

基于c#图像灰度化、灰度反转、二值化的实现方法详解

精品推荐
分类导航