Método 1: Convert.ParaDataTempo (string)
O formato string tem requisitos, deve ser yyy-MM-dd hh:mm:ss
================================================
Método 2: Convert.ParaData(string, IFormatProvider)
DateTime dt;
DateTimeFormatInfo dtFormat = novo System.GlobalizationDateTimeFormatInfo();
dtFormat.ShortDatePattern = "yyyy/MM/dd";
dt = Convert.ToDateTime("2011/05/26", dtFormat);
================================================
Método 2: DateTime.ParseExact()
string dateString = "20110526";
DateTimeDt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
ou
DateTimeDt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
Com informações de referência:
CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("en-US"); string format = "ddd MMM d HH:mm:ss zz00 yyyy"; stringValor = DataTempo.Agora.ParaString(format, cultureInfo); Pega a string de datas DateTimetimetime = DateTime.ParseExact("Qua 25 Ago 16:28:03 +0800 2010", format, cultureInfo); Converter cadeias em datas
outro
Formato de data: yyyyMMdd HH:mm:ss (note que essa string é muito estritamente baseada em casos) yyyy: representa o ano MM: Representa o mês DD: significa céu HH: Representa Horas (relógio de 24 horas) MM: representa minutos SS: significa segundos DataTempo.Agora.ParaTempoCurtoCorda() DateTimedt = DateTime.Now; DT. ToString(); //2005-11-5 13:21:25 DT. ToFileTime(). ToString(); //127756416859912816 DT. ToFileTimeUtc(). ToString(); //127756704859912816 DT. ToLocalTime(). ToString(); //2005-11-5 21:21:25 DT. ToLongDataString(). ToString(); 5 de novembro de 2005 DT. ToLongTimeString(). ToString(); //13:21:25 DT. ToOADate(). ToString(); //38661.5565508218 DT. ToShortDataString(). ToString(); //2005-11-5 DT. ToShortTimeString(). ToString(); //13:21 DT. ParaTempoUniversal(). ToString(); //2005-11-5 5:21:25 DT. Year.ToString(); //2005 DT. Data.ParaCorda(); //2005-11-5 0:00:00 DT. DayOfWeek.ToString(); Sábado DT. DiaDeAno.ParaFio(); //309 DT. Hora.ParaFio(); //13 DT. Milissegundo.ToString(); //441 DT. Minuto.ParaFio(); //30 DT. Mês.ParaFio(); //11 DT. Segundo.ToString(); //28 DT. Ticks.ToString(); //632667942284412864 DT. TimeOfDay.ToString(); //13:30:28.4412864 DT. ToString(); //2005-11-5 13:47:04 DT. AddYears(1). ToString(); //2006-11-5 13:47:04 DT. AddDays (1.1). ToString(); //2005-11-6 16:11:04 DT. AddHours(1.1). ToString(); //2005-11-5 14:53:04 DT. AddMilissegundos(1,1). ToString(); //2005-11-5 13:47:04 DT. AddMonths(1). ToString(); //2005-12-5 13:47:04 DT. AddSeconds (1.1). ToString(); //2005-11-5 13:47:05 DT. AddMinutes(1.1). ToString(); //2005-11-5 13:48:10 DT. AddTicks(1000). ToString(); //2005-11-5 13:47:04 DT. CompareTo(dt). ToString(); //0 DT. Adicionar(?). ToString(); O ponto de interrogação é um período de tempo DT. Igual("2005-11-6 16:11:04"). ToString(); Falso DT. Igual (dt). ToString(); Verdade DT. GetHashCode(). ToString(); //1474088234 DT. GetType(). ToString(); System.DateTime DT. GetTypeCode(). ToString(); DataHora
DT. GetDateTimeFormats('s')[0]. ToString(); 2005-11-05T14:06:25 DT. GetDateTimeFormats('t')[0]. ToString(); //14:06 DT. GetDateTimeFormats('y')[0]. ToString(); Novembro de 2005 DT. GetDateTimeFormats('D')[0]. ToString(); 5 de novembro de 2005 DT. GetDateTimeFormats('D')[1]. ToString(); //2005 11 05 DT. GetDateTimeFormats('D')[2]. ToString(); Sábado 2005 11h05 DT. GetDateTimeFormats('D')[3]. ToString(); Sábado, 5 de novembro de 2005 DT. GetDateTimeFormats('M')[0]. ToString(); 5 de novembro DT. GetDateTimeFormats('f')[0]. ToString(); 05 de novembro de 2005, 14:06 DT. GetDateTimeFormats('g')[0]. ToString(); //2005-11-5 14:06 DT. GetDateTimeFormats('r')[0]. ToString(); Sáb, 05 Nov 2005 14:06:25 GMT fio. Format("{0:d}",dt); //2005-11-5 fio. Format("{0}",dt); 5 de novembro de 2005 fio. Format("{0:f}",dt); 05 de novembro de 2005, 14:23 fio. Format("{0:F}",dt); 14:23:23, 5 Nov 2005 fio. Format("{0:g}",dt); //2005-11-5 14:23 fio. Format("{0:G}",dt); //2005-11-5 14:23:23 fio. Format("{0:M}",dt); 5 de novembro fio. Format("{0:R}",dt); Sáb, 05 Nov 2005 14:23:23 GMT fio. Format("{0:s}",dt); 2005-11-05T14:23:23 fio. Format("{0:t}",dt); //14:23 fio. Format("{0:T}",dt); //14:23:23 fio. Format("{0:u}",dt); 05-11-2005 14:23:23Z fio. Format("{0:U}",dt); 05 de novembro de 2005, 6:23:23 fio. Format("{0:Y}",dt); Novembro de 2005 fio. Format("{0}",dt); //2005-11-5 14:23:23 fio. Format("{0:yyyMMddHHmmssffff}",dt); Calcule a diferença em dias entre 2 datas ----------------------------------------------- DateTime dt1 = Convert.DateTime("2007-8-1"); DateTime dt2 = Convert.DateTime("2007-8-15"); Intervalo de tempo = dt2. Subtrair (dt1); int dayDiff = span. Dias + 1; Calcule o número de dias em um determinado ano e mês ----------------------------------------------- dias int: = DataTempo.DiasInMês(2007, 8); dias = 31; Adicione um dia à data e subtraia um dia ----------------------------------------------- DateTimed dt =DateTime.Now; DT. AddDays(1); Adicione um dia DT. AddDays(-1); Um dia a menos Outros anos são parecidos... Converter funções de data no Oracle SQL ----------------------------------------------- to_date("2007-6-6",'YYY-MM-DD"); to_date("2007/6/6", 'yyyy/mm/dd"); |