1

I have this date : 25/11/2019 14:20:17 CET I can't find a way to convert it to a datetime. Is there a generic way to convert properly several different date syntaxes?

            try
            {
                mailMessage.MessageSentTime = DateTime.Parse(attValue);
            }
            catch
            {
                string[] formats = new[]
                        {
                            "ddd, dd MMM yyyy HH:mm:ss CEST",
                            "ddd, dd MMM yyyy HH:mm:ss CET",
                            "dd/mm/yyyy HH:mm:ss CET"
                        };
                mailMessage.MessageSentTime = DateTime.ParseExact(attValue,
                                formats,
                                CultureInfo.InvariantCulture,
                                DateTimeStyles.None);
            }

String '25/11/2019 14:20:17 CET' was not recognized as a valid DateTime.

CloudAnywhere
  • 669
  • 1
  • 11
  • 27
  • Perhaps this will help: [parse-datetime-with-time-zone-of-form-pst-cest-utc-etc](https://stackoverflow.com/questions/241789/parse-datetime-with-time-zone-of-form-pst-cest-utc-etc) – Ryan Wilson Dec 01 '22 at 17:42
  • You forgot to include other formats like mm/dd/yyyy (as you never know if month is before day or after). Please reconsider your requirements unless you actually have to parse random texts... – Alexei Levenkov Dec 01 '22 at 17:46

0 Answers0