|
| 1 | +using System; |
| 2 | +using System.Globalization; |
| 3 | + |
| 4 | +public class DateTimeParse3 |
| 5 | +{ |
| 6 | + public static void ParseWithThreeArgs() |
| 7 | + { |
| 8 | + string dateString; |
| 9 | + CultureInfo culture; |
| 10 | + DateTimeStyles styles; |
| 11 | + DateTime result; |
| 12 | + |
| 13 | + // Parse a date and time with no styles. |
| 14 | + dateString = "03/01/2009 10:00 AM"; |
| 15 | + culture = CultureInfo.CreateSpecificCulture("en-US"); |
| 16 | + styles = DateTimeStyles.None; |
| 17 | + try |
| 18 | + { |
| 19 | + result = DateTime.Parse(dateString, culture, styles); |
| 20 | + Console.WriteLine("{0} converted to {1} {2}.", |
| 21 | + dateString, result, result.Kind.ToString()); |
| 22 | + } |
| 23 | + catch (FormatException) |
| 24 | + { |
| 25 | + Console.WriteLine("Unable to convert {0} to a date and time.", |
| 26 | + dateString); |
| 27 | + } |
| 28 | + |
| 29 | + // Parse the same date and time with the AssumeLocal style. |
| 30 | + styles = DateTimeStyles.AssumeLocal; |
| 31 | + try |
| 32 | + { |
| 33 | + result = DateTime.Parse(dateString, culture, styles); |
| 34 | + Console.WriteLine("{0} converted to {1} {2}.", |
| 35 | + dateString, result, result.Kind.ToString()); |
| 36 | + } |
| 37 | + catch (FormatException) |
| 38 | + { |
| 39 | + Console.WriteLine("Unable to convert {0} to a date and time.", dateString); |
| 40 | + } |
| 41 | + |
| 42 | + // Parse a date and time that is assumed to be local. |
| 43 | + // This time is five hours behind UTC. The local system's time zone is |
| 44 | + // eight hours behind UTC. |
| 45 | + dateString = "2009/03/01T10:00:00-5:00"; |
| 46 | + styles = DateTimeStyles.AssumeLocal; |
| 47 | + try |
| 48 | + { |
| 49 | + result = DateTime.Parse(dateString, culture, styles); |
| 50 | + Console.WriteLine("{0} converted to {1} {2}.", |
| 51 | + dateString, result, result.Kind.ToString()); |
| 52 | + } |
| 53 | + catch (FormatException) |
| 54 | + { |
| 55 | + Console.WriteLine("Unable to convert {0} to a date and time.", dateString); |
| 56 | + } |
| 57 | + |
| 58 | + // Attempt to convert a string in improper ISO 8601 format. |
| 59 | + dateString = "03/01/2009T10:00:00-5:00"; |
| 60 | + try |
| 61 | + { |
| 62 | + result = DateTime.Parse(dateString, culture, styles); |
| 63 | + Console.WriteLine("{0} converted to {1} {2}.", |
| 64 | + dateString, result, result.Kind.ToString()); |
| 65 | + } |
| 66 | + catch (FormatException) |
| 67 | + { |
| 68 | + Console.WriteLine("Unable to convert {0} to a date and time.", dateString); |
| 69 | + } |
| 70 | + |
| 71 | + // Assume a date and time string formatted for the fr-FR culture is the local |
| 72 | + // time and convert it to UTC. |
| 73 | + dateString = "2008-03-01 10:00"; |
| 74 | + culture = CultureInfo.CreateSpecificCulture("fr-FR"); |
| 75 | + styles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal; |
| 76 | + try |
| 77 | + { |
| 78 | + result = DateTime.Parse(dateString, culture, styles); |
| 79 | + Console.WriteLine("{0} converted to {1} {2}.", |
| 80 | + dateString, result, result.Kind.ToString()); |
| 81 | + } |
| 82 | + catch (FormatException) |
| 83 | + { |
| 84 | + Console.WriteLine("Unable to convert {0} to a date and time.", dateString); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +// The example displays the following output to the console: |
| 89 | +// 03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Unspecified. |
| 90 | +// 03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Local. |
| 91 | +// 2009/03/01T10:00:00-5:00 converted to 3/1/2009 7:00:00 AM Local. |
| 92 | +// Unable to convert 03/01/2009T10:00:00-5:00 to a date and time. |
| 93 | +// 2008-03-01 10:00 converted to 3/1/2008 6:00:00 PM Utc. |
| 94 | + |
| 95 | + |
0 commit comments