本文整理匯總了C#中System.DateTime.ParseExact方法的典型用法代碼示例。如果您正苦於以下問題:C# DateTime.ParseExact方法的具體用法?C# DateTime.ParseExact怎麽用?C# DateTime.ParseExact使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.DateTime
的用法示例。
在下文中一共展示了DateTime.ParseExact方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
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);
}
// Parse a date that includes seconds and milliseconds
// by using the French (France) and invariant cultures.
dateString = "18/08/2015 06:30:15.006542";
format = "dd/MM/yyyy HH:mm:ss.ffffff";
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);
}
}
}
輸出:
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. 18/08/2015 06:30:15.006542 converts to 8/18/2015 6:30:15 AM.
示例2: Main
//引入命名空間
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo enUS = new CultureInfo("en-US");
string dateString;
DateTime dateValue;
// Parse date with no style flags.
dateString = " 5/01/2009 8:30 AM";
try {
dateValue = DateTime.ParseExact(dateString, "g", enUS, DateTimeStyles.None);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
// Allow a leading space in the date string.
try {
dateValue = DateTime.ParseExact(dateString, "g", enUS, DateTimeStyles.AllowLeadingWhite);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
// Use custom formats with M and MM.
dateString = "5/01/2009 09:00";
try {
dateValue = DateTime.ParseExact(dateString, "M/dd/yyyy hh:mm", enUS, DateTimeStyles.None);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
// Allow a leading space in the date string.
try {
dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, DateTimeStyles.None);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
// Parse a string with time zone information.
dateString = "05/01/2009 01:30:42 PM -05:00";
try {
dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, DateTimeStyles.None);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
// Allow a leading space in the date string.
try {
dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS,
DateTimeStyles.AdjustToUniversal);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
// Parse a string representing UTC.
dateString = "2008-06-11T16:11:20.0904778Z";
try {
dateValue = DateTime.ParseExact(dateString, "o", CultureInfo.InvariantCulture,
DateTimeStyles.None);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
try {
dateValue = DateTime.ParseExact(dateString, "o", CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
}
}
輸出:
' 5/01/2009 8:30 AM' is not in an acceptable format. Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified). Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified). '5/01/2009 09:00' is not in an acceptable format. Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local). Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc). Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local). Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).
示例3: Main
//引入命名空間
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
"M/d/yyyy h:mm", "M/d/yyyy h:mm",
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
"MM/d/yyyy HH:mm:ss.ffffff" };
string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM",
"5/1/2009 6:32:00", "05/01/2009 06:32",
"05/01/2009 06:32:00 PM", "05/01/2009 06:32:00",
"08/28/2015 16:17:39.125", "08/28/2015 16:17:39.125000" };
DateTime dateValue;
foreach (string dateString in dateStrings)
{
try {
dateValue = DateTime.ParseExact(dateString, formats,
new CultureInfo("en-US"),
DateTimeStyles.None);
Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
}
catch (FormatException) {
Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
}
}
}
}
輸出:
Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM. Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM. Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM. Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM. Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM. Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM. Unable to convert '08/28/2015 16:17:39.125' to a date. Converted '08/28/2015 16:17:39.125000' to 8/28/2015 4:17:39 PM.
示例4: Main
//引入命名空間
using System;
class MainClass
{
public static void Main(string[] args)
{
DateTime dt6 = DateTime.ParseExact("2:13:30 PM", "h:mm:ss tt", null);
Console.WriteLine(dt6);
}
}