String was not recognized as a valid DateTime

c#, c#-4.0

Solution

Convert.ToDateTime uses current culture information about DateTime format. Try something like this:

string format = "dd/MM/yyyy hh:mm tt";
string stringDate = DateTime.Now.ToString(format, CultureInfo.InvariantCulture);
DateTime dateTime = DateTime.ParseExact(stringDate, format, CultureInfo.InvariantCulture);

Problem

I'm receiving this error "String was not recognized as a valid DateTime" with the code below: ``` DateTimeOffSet dt=new DateTimeOffset(Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy hh:mm tt"))); ``` It works in DEV environment but not in Production. Could anyone please advice what's wrong with the code above? Thank you.

Original source