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.
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);
Why are you converting from DateTime
to string
and then back to DateTime
?
I think this should work fine:
DateTimeOffset dt = new DateTimeOffset(DateTime.Now);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With