user2483797
user2483797

Reputation: 169

DateTime conversion between different culture info

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

Answers (2)

Sudhakar Tillapudi
Sudhakar Tillapudi

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

Stephan B
Stephan B

Reputation: 3701

You are Parsing the string value of myDate using the given date format, use `myDate.ToString("dd-MMM-yyyy h:mm:ss tt") to convert a date to a string format.

Upvotes: 0

Related Questions