Reputation: 169
I want to create different conversion between countries and i am using c#. I am trying to convert a date time to another date time, format dd-mmm-yyyy.
CultureInfo ci = CultureInfo.CreateSpecificCulture(language.US); //en-us
DateTime dateStart= DateTime.ParseExact(myDate.ToString(),"dd-MMM-yyyy h:mm:ss tt", ci); //mydate: 12/01/2013 17:00:00 a.m.
And it gives error: String was not recognized as a valid DateTime. Please advice.
Upvotes: 2
Views: 2061
Reputation: 26209
1. your String Should contain AM
or PM
but not a.m
or p.m
2. your current Date String -> 12/01/2013 17:00:00 a.m.
is Wrong as it contains a.m
for 17th Hour.it should be -> 12/01/2013 17:00:00 PM
3. you can use System.Globalization.CultureInfo.InvariantCulture
as CutureInfo
to deal with different Cultures
.
4. if your Month is 3 letter word like JAN, FEB, DEC. etc., you can use MMM
insted of MM
as month custom format.
like this ->"dd/MMM/yyyy HH:mm:ss tt"
Solution 1: Try This: if your Month is Two Digit number
String myDate = "11/01/2013 17:00:00 PM";
DateTime dateStart = DateTime.ParseExact(myDate, "dd/MM/yyyy HH:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);
Solution 2: Try This: if your Month is Three Letter Word
String myDate = "11/DEC/2013 17:00:00 PM";
DateTime dateStart = DateTime.ParseExact(myDate, "dd/MMM/yyyy HH:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);
Upvotes: 2