C# DateTimeOffset ParseExact(String, String, IFormatProvider)
Description
DateTimeOffset ParseExact(String, String, IFormatProvider)
converts the specified string representation of a date and time
to its DateTimeOffset equivalent using the specified format and culture-specific
format information. The format of the string representation must match
the specified format exactly.
Syntax
DateTimeOffset.ParseExact(String, String, IFormatProvider)
has the following syntax.
public static DateTimeOffset ParseExact(
string input,/*from w w w . j av a 2s. com*/
string format,
IFormatProvider formatProvider
)
Parameters
DateTimeOffset.ParseExact(String, String, IFormatProvider)
has the following parameters.
input
- A string that contains a date and time to convert.format
- A format specifier that defines the expected format of input.formatProvider
- An object that supplies culture-specific formatting information about input.
Returns
DateTimeOffset.ParseExact(String, String, IFormatProvider)
method returns An object that is equivalent to the date and time that is contained in input
as specified by format and formatProvider.
Example
The following example uses the DateTimeOffset.ParseExact(String, String, IFormatProvider) method with standard and custom format specifiers and the invariant culture to parse several date and time strings.
using System;/*w w w. j ava 2s . c om*/
using System.Globalization;
public class MainClass{
public static void Main(String[] argv){
string dateString, format;
DateTimeOffset result;
CultureInfo provider = CultureInfo.InvariantCulture;
dateString = "06/15/2014";
format = "d";
try
{
result = DateTimeOffset.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 = "6/15/2014";
try
{
result = DateTimeOffset.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 code above generates the following result.