C# DateTimeOffset ParseExact(String, String, IFormatProvider, DateTimeStyles)
Description
DateTimeOffset ParseExact(String, String, IFormatProvider,
DateTimeStyles)
converts the specified string representation
of a date and time to its DateTimeOffset equivalent using the specified format,
culture-specific format information, and style. The format of the string
representation must match the specified format exactly.
Syntax
DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles)
has the following syntax.
public static DateTimeOffset ParseExact(
string input,/*ww w .jav a 2s . c om*/
string format,
IFormatProvider formatProvider,
DateTimeStyles styles
)
Parameters
DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles)
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.styles
- A bitwise combination of enumeration values that indicates the permitted format of input.
Returns
DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles)
method returns An object that is equivalent to the date and time that is contained in the input
parameter, as specified by the format, formatProvider, and styles parameters.
Example
The following example uses the DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) method with standard and custom format specifiers, the invariant culture, and various DateTimeStyles values to parse several date and time strings.
//from w ww. j av a 2 s . c o m
using System;
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,
DateTimeStyles.AssumeUniversal);
Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
Console.WriteLine("'{0}' is not in the correct format.", dateString);
}
dateString = " 06/15/2014";
try
{
result = DateTimeOffset.ParseExact(dateString, format, provider,
DateTimeStyles.AllowTrailingWhite);
Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
Console.WriteLine("'{0}' is not in the correct format.", dateString);
}
dateString = " 06/15/ 2014 15:15 -05:00";
format = "MM/dd/yyyy H:mm zzz";
try
{
result = DateTimeOffset.ParseExact(dateString, format, provider,
DateTimeStyles.AllowWhiteSpaces);
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.