2013-04-15 11 views
9

Desidero formattare la stringa di input nel formato MM/gg/aaaa hh: mm: ss in C#.
La stringa di input è in formato MM/dd/yyyy hh:mm:ss
Ad esempio: "04/30/2013 23:00"Formato datetime Problema: la stringa non è stata riconosciuta come DateTime valido

Ho provato Convert.ToDateTime() funzione, ma ritiene 4 come data e 3 come mese che è non quello che voglio. In realtà il mese è 04 e la data è 03.

Ho provato anche la funzione DateTime.ParseExact(), ma ottenendo l'eccezione.

sto ottenendo errore:

String was not recognized as a valid DateTime.

+2

potete inserire il codice sorgente effettivo che avete problemi con? – tafa

+0

Beh, come hai provato 'ParseExact'? Immagino che ti sia sfuggito qualcosa per il formato passato semplice suppongo che sarebbe 'HH' invece di' hh' dato che hai 24 ore nel formato – V4Vendetta

+0

usi il datetime picker? –

risposta

12

La stringa data ora doesn' t contiene secondi. È necessario rifletterlo nel formato (rimuovere lo :ss).
Inoltre, è necessario specificare H invece di h se si utilizza 24 volte all'ora:

DateTime.ParseExact("04/30/2013 23:00", "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture) 

Vedi qui per maggiori informazioni:

Custom Date and Time Format Strings

+0

'str =" 6 "; dtm = DateTime.ParseExact (str, "d", CultureInfo.InvariantCulture); "sta fallendo per me. Qualche idea? – Si8

+0

@ Si8 È perché "d" è una stringa di formato standard (data breve). Puoi invece usare "% d". Vedi qui: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx#UsingSingleSpecifiers – Botz3000

+0

ottima risposta risparmi la giornata grazie –

3

provare questo:

string strTime = "04/30/2013 23:00"; 
DateTime dtTime; 
if(DateTime.TryParseExact(strTime, "MM/dd/yyyy HH:mm", 
    System.Globalization.CultureInfo.InvariantCulture, 
    System.Globalization.DateTimeStyles.None, out dtTime)) 
{ 
    Console.WriteLine(dtTime); 
} 
5

È possibile utilizzare il metodo DateTime.ParseExact().

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

DateTime date = DateTime.ParseExact("04/30/2013 23:00", 
            "MM/dd/yyyy HH:mm", 
            CultureInfo.InvariantCulture); 

Ecco un DEMO.

hh è per 12 ore 01-12, HH è per 24 ore dalle 00 alle 23.

Per ulteriori informazioni, controllare Custom Date and Time Format Strings

+2

Grazie. Funziona per me :) – Priya

+0

@Priya Siete i benvenuti ';)' –

0
DateTime dt1 = DateTime.ParseExact([YourDate], "dd-MM-yyyy HH:mm:ss", 
              CultureInfo.InvariantCulture); 

Nota l'uso di HH (Orologio a 24 ore) anziché hh (orologio a 12 ore) e l'uso di InvariantCulture perché alcune culture utilizzano separatori diversi dalla barra.

Ad esempio, se la cultura è de-DE, il formato "gg/MM/aaaa" prevede il periodo come separatore (31.01.2011).

0

Sotto codice ha funzionato per me:

string _stDate = Convert.ToDateTime(DateTime.Today.AddMonths(-12)).ToString("MM/dd/yyyy"); 
String format ="MM/dd/yyyy"; 
IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true); 
DateTime _Startdate = DateTime.ParseExact(_stDate, format, culture); 
Problemi correlati