使用DateTime的ParseExact方法实现特殊日期时间的方法详解(转)
2013-08-14 12:16
831 查看
本篇文章是对使用DateTime的ParseExact方法实现特殊日期时间的方法进行了详细的分析介绍,需要的朋友参考下
今天遇到一个特别的需求,需要从下面的字符串中转换成一个DateTime对象:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
主要问题是这个时间不是标准的时间,而是自定义的格式,即开头是月-日,然后是时间。
使用最常用的DateTime.Parse(string dateTimeStr)无法转换,问题就在于这个自定义格式上。
搜索了之后,我找到了下面的方法:
使用例子如下:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
使用效果如下:
如果你使用的其它特殊语言,比如美国或者日文啥的,最后的参数你可能需要获取下对应的Culture。
注意:
•如果dateTimeStr或者format 是null,会抛出ArgumentNullException异常。
•如果dateTimeStr或者format 是空字符串,则抛出FormatException异常。
相关资料:
http://hi.baidu.com/tracyjay/item/9c3208ab3bd82fff14329bac
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Exception
Condition
ArgumentNullException
s or format is null.
FormatException
s or format is an empty string.
-or-
s does not contain a date and time that corresponds to the pattern specified in format.
-or-
The hour component and the AM/PM designator in s do not agree.
The DateTime.ParseExact(String, String, IFormatProvider) method parses the string representation of a date, which must be in the format defined by the formatparameter. It also requires that the <Date> and <Time> elements of the string representation of a date and time appear in the order specified by format, and that shave no white space other than that permitted by format. If format defines a date with no time element and the parse operation succeeds, the resulting DateTimevalue has a time of midnight (00:00:00). If format defines a time with no date element and the parse operation succeeds, the resulting DateTime value has a date ofDateTime.Now.Date.
If s does not represent a time in a particular time zone and the parse operation succeeds, the Kind property of the returned DateTime value isDateTimeKind.Unspecified. If s does represent the time in a particular time zone and format allows time zone information to be present (for example, if format is equal to the "o", "r", or "u" standard format specifiers, or if it contains the "z", "zz", or "zzz" custom format specifiers), the Kind property of the returned DateTimevalue is DateTimeKind.Local.
The format parameter is a string that contains either a single standard format specifier, or one or more custom format specifiers that define the required format of s. For details about valid formatting codes, see Standard Date and Time Format Strings or Custom Date and Time Format Strings.
Note
If format is a custom format pattern that does not include date or time separators (such as "yyyyMMdd HHmm"), use the invariant culture for the providerparameter and the widest form of each custom format specifier. For example, if you want to specify hours in the format pattern, specify the wider form, "HH", instead of the narrower form, "H".
The particular date and time symbols and strings (such as names of the days of the week in a particular language) used in s are defined by the provider parameter, as is the precise format of s if format is a standard format specifier string. The provider parameter can be any of the following:
A CultureInfo object that represents the culture used to interpret s. The DateTimeFormatInfo object returned by its DateTimeFormat property defines the symbols and formatting in s.
A DateTimeFormatInfo object that defines the format of date and time data.
A custom IFormatProvider implementation whose GetFormat method returns either the CultureInfo object or the DateTimeFormatInfo object that provides formatting information.
If provider is null, the CultureInfo object that corresponds to the current culture is used.
Notes to Callers
In the .NET Framework 4, the ParseExact method throws a FormatException if the string to be parsed contains an hour component and an AM/PM designator that are not in agreement. In the .NET Framework 3.5 and earlier versions, the AM/PM designator is ignored.
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
今天遇到一个特别的需求,需要从下面的字符串中转换成一个DateTime对象:
[07-13 15:50:42]
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
主要问题是这个时间不是标准的时间,而是自定义的格式,即开头是月-日,然后是时间。
使用最常用的DateTime.Parse(string dateTimeStr)无法转换,问题就在于这个自定义格式上。
搜索了之后,我找到了下面的方法:
public static DateTime ParseExact( string s, string format, IFormatProvider provider )
使用例子如下:
var dateTimeStr = "07-13 15:50:42"; var dateTime = DateTime.ParseExact(dateTimeStr, "MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
使用效果如下:
如果你使用的其它特殊语言,比如美国或者日文啥的,最后的参数你可能需要获取下对应的Culture。
注意:
•如果dateTimeStr或者format 是null,会抛出ArgumentNullException异常。
•如果dateTimeStr或者format 是空字符串,则抛出FormatException异常。
相关资料:
http://hi.baidu.com/tracyjay/item/9c3208ab3bd82fff14329bac
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
DateTime.ParseExact Method (String, String, IFormatProvider)
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.public static DateTime ParseExact( string s, string format, IFormatProvider provider )
Parameters s Type: System.String A string that contains a date and time to convert. format Type: System.String A format specifier that defines the required format of s. For more information, see the Remarks section. provider Type: System.IFormatProvider An object that supplies culture-specific format information about s. Return Value Type: System.DateTime An object that is equivalent to the date and time contained in s, as specified by format and provider.
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Exception
Condition
ArgumentNullException
s or format is null.
FormatException
s or format is an empty string.
-or-
s does not contain a date and time that corresponds to the pattern specified in format.
-or-
The hour component and the AM/PM designator in s do not agree.
The DateTime.ParseExact(String, String, IFormatProvider) method parses the string representation of a date, which must be in the format defined by the formatparameter. It also requires that the <Date> and <Time> elements of the string representation of a date and time appear in the order specified by format, and that shave no white space other than that permitted by format. If format defines a date with no time element and the parse operation succeeds, the resulting DateTimevalue has a time of midnight (00:00:00). If format defines a time with no date element and the parse operation succeeds, the resulting DateTime value has a date ofDateTime.Now.Date.
If s does not represent a time in a particular time zone and the parse operation succeeds, the Kind property of the returned DateTime value isDateTimeKind.Unspecified. If s does represent the time in a particular time zone and format allows time zone information to be present (for example, if format is equal to the "o", "r", or "u" standard format specifiers, or if it contains the "z", "zz", or "zzz" custom format specifiers), the Kind property of the returned DateTimevalue is DateTimeKind.Local.
The format parameter is a string that contains either a single standard format specifier, or one or more custom format specifiers that define the required format of s. For details about valid formatting codes, see Standard Date and Time Format Strings or Custom Date and Time Format Strings.
Note
If format is a custom format pattern that does not include date or time separators (such as "yyyyMMdd HHmm"), use the invariant culture for the providerparameter and the widest form of each custom format specifier. For example, if you want to specify hours in the format pattern, specify the wider form, "HH", instead of the narrower form, "H".
The particular date and time symbols and strings (such as names of the days of the week in a particular language) used in s are defined by the provider parameter, as is the precise format of s if format is a standard format specifier string. The provider parameter can be any of the following:
A CultureInfo object that represents the culture used to interpret s. The DateTimeFormatInfo object returned by its DateTimeFormat property defines the symbols and formatting in s.
A DateTimeFormatInfo object that defines the format of date and time data.
A custom IFormatProvider implementation whose GetFormat method returns either the CultureInfo object or the DateTimeFormatInfo object that provides formatting information.
If provider is null, the CultureInfo object that corresponds to the current culture is used.
Notes to Callers
In the .NET Framework 4, the ParseExact method throws a FormatException if the string to be parsed contains an hour component and an AM/PM designator that are not in agreement. In the .NET Framework 3.5 and earlier versions, the AM/PM designator is ignored.
using System; using System.Globalization; public class Example { public static void Main() { string dateString, format; DateTime result; CultureInfo provider = CultureInfo.InvariantCulture; // Parse date-only value with invariant culture. dateString = "06/15/2008"; format = "d"; try { result = DateTime.ParseExact(dateString, format, provider); Console.WriteLine("{0} converts to {1}.", dateString, result.ToString()); } catch (FormatException) { Console.WriteLine("{0} is not in the correct format.", dateString); } // Parse date-only value without leading zero in month using "d" format. // Should throw a FormatException because standard short date pattern of // invariant culture requires two-digit month. dateString = "6/15/2008"; try { result = DateTime.ParseExact(dateString, format, provider); Console.WriteLine("{0} converts to {1}.", dateString, result.ToString()); } catch (FormatException) { Console.WriteLine("{0} is not in the correct format.", dateString); } // Parse date and time with custom specifier. dateString = "Sun 15 Jun 2008 8:30 AM -06:00"; format = "ddd dd MMM yyyy h:mm tt zzz"; try { result = DateTime.ParseExact(dateString, format, provider); Console.WriteLine("{0} converts to {1}.", dateString, result.ToString()); } catch (FormatException) { Console.WriteLine("{0} is not in the correct format.", dateString); } // Parse date and time with offset but without offset's minutes. // Should throw a FormatException because "zzz" specifier requires leading // zero in hours. dateString = "Sun 15 Jun 2008 8:30 AM -06"; try { result = DateTime.ParseExact(dateString, format, provider); Console.WriteLine("{0} converts to {1}.", dateString, result.ToString()); } catch (FormatException) { Console.WriteLine("{0} is not in the correct format.", dateString); } dateString = "15/06/2008 08:30"; format = "g"; provider = new CultureInfo("fr-FR"); try { result = DateTime.ParseExact(dateString, format, provider); Console.WriteLine("{0} converts to {1}.", dateString, result.ToString()); } catch (FormatException) { Console.WriteLine("{0} is not in the correct format.", dateString); } } } // The example displays the following output: // 06/15/2008 converts to 6/15/2008 12:00:00 AM. // 6/15/2008 is not in the correct format. // Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 7:30:00 AM. // Sun 15 Jun 2008 8:30 AM -06 is not in the correct format. // 15/06/2008 08:30 converts to 6/15/2008 8:30:00 AM.
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
相关文章推荐
- 使用DateTime的ParseExact方法实现特殊日期时间的方法详解
- 用DateTime的ParseExact方法解析特殊的日期时间
- C#中用DateTime的ParseExact方法解析日期时间(excel中使用系统默认的日期格式)
- c#美味:用DateTime的ParseExact方法解析特殊的日期时间
- [转]c#:用DateTime的ParseExact方法解析特殊的日期时间
- 日期转换(用DateTime的ParseExact方法解析特殊的日期时间)
- 使用Smarty 获取当前日期时间和格式化日期时间的方法详解
- 使用Smarty 获取当前日期时间和格式化日期时间的方法详解
- DateTime.ToString格式化日期,使用DateDiff方法获取日期时间的间隔数
- Java 使用jdk8的新特性LocalDate和LocalDateTime 实现当前时间,日期加减,是否闰年,时间戳,时间线程每秒改变一次
- Bootstrap3 日期+时间选择控件 datetimepicker的使用方法说明
- VC控件DateTimePicker使用方法 ,获取日期和时间
- LocalDate, LocalTime, LocalDateTime, and Instant表示时间点的日期-时间类的通用方法
- 使用jQuery方法获取当前日期和时间
- 详解Java注解的实现与使用方法
- bootstrap之datetimepicker(日期时间选择器)插件详细使用方法
- 通常在C#中判断两个日期是否相等可使用DateTime的方法
- struct tm 和 time_t 时间和日期的使用方法(转)
- anguar js 日期和时间选择插件 angular-datetimepicker 使用时注意事项
- linux命令详解date使用方法(计算母亲节和父亲节日期脚本示例)