|
Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
|
Tradução
Original
|
Método DateTime.Parse (String)
Namespace: System
Assembly: mscorlib (em mscorlib.dll)
Parâmetros
- s
- Tipo: System.String
Uma cadeia de caracteres que contém uma data e hora converter.
| Exceção | Condição |
|---|---|
| ArgumentNullException | |
| FormatException |
Uma cadeia de caracteres com um componente de tempo. Uma cadeia de caracteres com uma data mas nenhum componente de tempo. Uma cadeia de caracteres com um tempo mas nenhum componente de data. Uma cadeia de caracteres que inclui informações de zona de tempo e atendem a ISO 8601. Por exemplo, o primeiro as duas cadeias de caracteres designa o Tempo Universal Coordenado (UTC); o segundo designa o horário em um fuso horário sete horas anteriores ao UTC: 2008-11-01T19: 35:00.0000000Z 2008-11-01T19: 35:00.0000000-07: 00 Uma cadeia de caracteres que inclui o designador GMT e atendem a RFC um formato de 1123 horas. Por exemplo: Sat:, o 1º de novembro de 2008 19:35GMT 00 Uma cadeia de caracteres que inclui a data e hora junto com informações de deslocamento da zona de tempo. Por exemplo: 03/01/2009 05:42:00 -5:00
Importante |
|---|
Importante |
|---|
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 {0} to {1} time {2}.", dateString, convertedDate.Kind.ToString(), convertedDate); } // These calls to the DateTime.Parse method display the following output: // 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.
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); } } } // The example displays the following output to the console: // '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.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Função Server Core sem suporte), Windows Server 2008 R2 (Função Server Core com suporte com o SP1 ou posterior, Itanium sem suporte)
O .NET Framework não oferece suporte a todas as versões de cada plataforma. Para obter uma lista das versões com suporte, consulte .Requisitos de sistema do NET Framework.
Importante