本文整理匯總了C#中System.DateTime.Parse方法的典型用法代碼示例。如果您正苦於以下問題:C# DateTime.Parse方法的具體用法?C# DateTime.Parse怎麽用?C# DateTime.Parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.DateTime
的用法示例。
在下文中一共展示了DateTime.Parse方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
public class Example
{
public static void Main()
{
(string dateAsString, string description)[] dateInfo = { ("08/18/2018 07:22:16", "String with a date and time component"),
("08/18/2018", "String with a date component only"),
("8/2018", "String with a month and year component only"),
("8/18", "String with a month and day component only"),
("07:22:16", "String with a time component only"),
("7 PM", "String with an hour and AM/PM designator only"),
("2018-08-18T07:22:16.0000000Z", "UTC string that conforms to ISO 8601"),
("2018-08-18T07:22:16.0000000-07:00", "Non-UTC string that conforms to ISO 8601"),
("Sat, 18 Aug 2018 07:22:16 GMT", "String that conforms to RFC 1123"),
("08/18/2018 07:22:16 -5:00", "String with date, time, and time zone information" ) };
Console.WriteLine($"Today is {DateTime.Now:d}\n");
foreach (var item in dateInfo) {
Console.WriteLine($"{item.description + ":",-52} '{item.dateAsString}' --> {DateTime.Parse(item.dateAsString)}");
}
}
}
輸出:
Today is 2/22/2018 String with a date and time component: '08/18/2018 07:22:16' --> 8/18/2018 7:22:16 AM String with a date component only: '08/18/2018' --> 8/18/2018 12:00:00 AM String with a month and year component only: '8/2018' --> 8/1/2018 12:00:00 AM String with a month and day component only: '8/18' --> 8/18/2018 12:00:00 AM String with a time component only: '07:22:16' --> 2/22/2018 7:22:16 AM String with an hour and AM/PM designator only: '7 PM' --> 2/22/2018 7:00:00 PM UTC string that conforms to ISO 8601: '2018-08-18T07:22:16.0000000Z' --> 8/18/2018 12:22:16 AM Non-UTC string that conforms to ISO 8601: '2018-08-18T07:22:16.0000000-07:00' --> 8/18/2018 7:22:16 AM String that conforms to RFC 1123: 'Sat, 18 Aug 2018 07:22:16 GMT' --> 8/18/2018 12:22:16 AM String with date, time, and time zone information: '08/18/2018 07:22:16 -5:00' --> 8/18/2018 5:22:16 AM
示例2: Main
//引入命名空間
using System;
public class Example
{
public static void Main()
{
string[] dateStrings = {"2008-05-01T07:34:42-5:00",
"2008-05-01 7:34:42Z",
"Thu, 01 May 2008 07:34:42 GMT"};
foreach (string dateString in dateStrings)
{
DateTime convertedDate = DateTime.Parse(dateString);
Console.WriteLine($"Converted {dateString} to {convertedDate.Kind} time {convertedDate}");
}
}
}
輸出:
Converted 2008-05-01T07:34:42-5:00 to Local time 5/1/2008 5:34:42 AM Converted 2008-05-01 7:34:42Z to Local time 5/1/2008 12:34:42 AM Converted Thu, 01 May 2008 07:34:42 GMT to Local time 5/1/2008 12:34:42 AM
示例3: foreach
string[] formattedDates = { "2008-09-15T09:30:41.7752486-07:00",
"2008-09-15T09:30:41.7752486Z",
"2008-09-15T09:30:41.7752486",
"2008-09-15T09:30:41.7752486-04:00",
"Mon, 15 Sep 2008 09:30:41 GMT" };
foreach (string formattedDate in formattedDates)
{
Console.WriteLine(formattedDate);
DateTime roundtripDate = DateTime.Parse(formattedDate, null,
DateTimeStyles.RoundtripKind);
Console.WriteLine($" With RoundtripKind flag: {roundtripDate} {roundtripDate.Kind} time.");
DateTime noRoundtripDate = DateTime.Parse(formattedDate, null,
DateTimeStyles.None);
Console.WriteLine($" Without RoundtripKind flag: {noRoundtripDate} {noRoundtripDate.Kind} time.");
}
輸出:
2008-09-15T09:30:41.7752486-07:00 With RoundtripKind flag: 9/15/2008 9:30:41 AM Local time. Without RoundtripKind flag: 9/15/2008 9:30:41 AM Local time. 2008-09-15T09:30:41.7752486Z With RoundtripKind flag: 9/15/2008 9:30:41 AM Utc time. Without RoundtripKind flag: 9/15/2008 2:30:41 AM Local time. 2008-09-15T09:30:41.7752486 With RoundtripKind flag: 9/15/2008 9:30:41 AM Unspecified time. Without RoundtripKind flag: 9/15/2008 9:30:41 AM Unspecified time. 2008-09-15T09:30:41.7752486-04:00 With RoundtripKind flag: 9/15/2008 6:30:41 AM Local time. Without RoundtripKind flag: 9/15/2008 6:30:41 AM Local time. Mon, 15 Sep 2008 09:30:41 GMT With RoundtripKind flag: 9/15/2008 9:30:41 AM Utc time. Without RoundtripKind flag: 9/15/2008 2:30:41 AM Local time.
示例4: Main
//引入命名空間
using System;
using System.Globalization;
public class DateTimeParser
{
public static void Main()
{
// Assume the current culture is en-US.
// The date is February 16, 2008, 12 hours, 15 minutes and 12 seconds.
// Use standard en-US date and time value
DateTime dateValue;
string dateString = "2/16/2008 12:15:12 PM";
try {
dateValue = DateTime.Parse(dateString);
Console.WriteLine("'{0}' converted to {1}.", dateString, dateValue);
}
catch (FormatException) {
Console.WriteLine("Unable to convert '{0}'.", dateString);
}
// Reverse month and day to conform to the fr-FR culture.
// The date is February 16, 2008, 12 hours, 15 minutes and 12 seconds.
dateString = "16/02/2008 12:15:12";
try {
dateValue = DateTime.Parse(dateString);
Console.WriteLine("'{0}' converted to {1}.", dateString, dateValue);
}
catch (FormatException) {
Console.WriteLine("Unable to convert '{0}'.", dateString);
}
// Call another overload of Parse to successfully convert string
// formatted according to conventions of fr-FR culture.
try {
dateValue = DateTime.Parse(dateString, new CultureInfo("fr-FR", false));
Console.WriteLine("'{0}' converted to {1}.", dateString, dateValue);
}
catch (FormatException) {
Console.WriteLine("Unable to convert '{0}'.", dateString);
}
// Parse string with date but no time component.
dateString = "2/16/2008";
try {
dateValue = DateTime.Parse(dateString);
Console.WriteLine("'{0}' converted to {1}.", dateString, dateValue);
}
catch (FormatException) {
Console.WriteLine("Unable to convert '{0}'.", dateString);
}
}
}
輸出:
'2/16/2008 12:15:12 PM' converted to 2/16/2008 12:15:12 PM. Unable to convert '16/02/2008 12:15:12'. '16/02/2008 12:15:12' converted to 2/16/2008 12:15:12 PM. '2/16/2008' converted to 2/16/2008 12:00:00 AM.
示例5: Main
//引入命名空間
using System;
using System.Globalization;
public class ParseDate
{
public static void Main()
{
// Define cultures to be used to parse dates.
CultureInfo[] cultures = {CultureInfo.CreateSpecificCulture("en-US"),
CultureInfo.CreateSpecificCulture("fr-FR"),
CultureInfo.CreateSpecificCulture("de-DE")};
// Define string representations of a date to be parsed.
string[] dateStrings = {"01/10/2009 7:34 PM",
"10.01.2009 19:34",
"10-1-2009 19:34" };
// Parse dates using each culture.
foreach (CultureInfo culture in cultures)
{
DateTime dateValue;
Console.WriteLine("Attempted conversions using {0} culture.",
culture.Name);
foreach (string dateString in dateStrings)
{
try {
dateValue = DateTime.Parse(dateString, culture);
Console.WriteLine(" Converted '{0}' to {1}.",
dateString, dateValue.ToString("f", culture));
}
catch (FormatException) {
Console.WriteLine(" Unable to convert '{0}' for culture {1}.",
dateString, culture.Name);
}
}
Console.WriteLine();
}
}
}
輸出:
Attempted conversions using en-US culture. Converted '01/10/2009 7:34 PM' to Saturday, January 10, 2009 7:34 PM. Converted '10.01.2009 19:34' to Thursday, October 01, 2009 7:34 PM. Converted '10-1-2009 19:34' to Thursday, October 01, 2009 7:34 PM. Attempted conversions using fr-FR culture. Converted '01/10/2009 7:34 PM' to jeudi 1 octobre 2009 19:34. Converted '10.01.2009 19:34' to samedi 10 janvier 2009 19:34. Converted '10-1-2009 19:34' to samedi 10 janvier 2009 19:34. Attempted conversions using de-DE culture. Converted '01/10/2009 7:34 PM' to Donnerstag, 1. Oktober 2009 19:34. Converted '10.01.2009 19:34' to Samstag, 10. Januar 2009 19:34. Converted '10-1-2009 19:34' to Samstag, 10. Januar 2009 19:34.
示例6: Main
//引入命名空間
using System;
using System.Globalization;
public class ParseDateExample
{
public static void Main()
{
string dateString;
CultureInfo culture ;
DateTimeStyles styles;
DateTime result;
// Parse a date and time with no styles.
dateString = "03/01/2009 10:00 AM";
culture = CultureInfo.CreateSpecificCulture("en-US");
styles = DateTimeStyles.None;
try {
result = DateTime.Parse(dateString, culture, styles);
Console.WriteLine("{0} converted to {1} {2}.",
dateString, result, result.Kind.ToString());
}
catch (FormatException) {
Console.WriteLine("Unable to convert {0} to a date and time.",
dateString);
}
// Parse the same date and time with the AssumeLocal style.
styles = DateTimeStyles.AssumeLocal;
try {
result = DateTime.Parse(dateString, culture, styles);
Console.WriteLine("{0} converted to {1} {2}.",
dateString, result, result.Kind.ToString());
}
catch (FormatException) {
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
}
// Parse a date and time that is assumed to be local.
// This time is five hours behind UTC. The local system's time zone is
// eight hours behind UTC.
dateString = "2009/03/01T10:00:00-5:00";
styles = DateTimeStyles.AssumeLocal;
try {
result = DateTime.Parse(dateString, culture, styles);
Console.WriteLine("{0} converted to {1} {2}.",
dateString, result, result.Kind.ToString());
}
catch (FormatException) {
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
}
// Attempt to convert a string in improper ISO 8601 format.
dateString = "03/01/2009T10:00:00-5:00";
try {
result = DateTime.Parse(dateString, culture, styles);
Console.WriteLine("{0} converted to {1} {2}.",
dateString, result, result.Kind.ToString());
}
catch (FormatException) {
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
}
// Assume a date and time string formatted for the fr-FR culture is the local
// time and convert it to UTC.
dateString = "2008-03-01 10:00";
culture = CultureInfo.CreateSpecificCulture("fr-FR");
styles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal;
try {
result = DateTime.Parse(dateString, culture, styles);
Console.WriteLine("{0} converted to {1} {2}.",
dateString, result, result.Kind.ToString());
}
catch (FormatException) {
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
}
}
}
輸出:
03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Unspecified. 03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Local. 2009/03/01T10:00:00-5:00 converted to 3/1/2009 7:00:00 AM Local. Unable to convert 03/01/2009T10:00:00-5:00 to a date and time. 2008-03-01 10:00 converted to 3/1/2008 6:00:00 PM Utc.
示例7: Main
//引入命名空間
using System;
class MainClass
{
public static void Main()
{
DateTime myDateTime = DateTime.Parse("1/11/2004 23:10:30");
TimeSpan myTimeSpan = new TimeSpan(1, 2, 4, 10);
DateTime myDateTime8 = myDateTime + myTimeSpan;
Console.WriteLine("myDateTime + myTimeSpan = " + myDateTime8);
}
}