Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CONVERT MM/DD/YYYY HH:MI:SS AM/PM to DD/MM/YYYY in C#

Tags:

c#

How can I convert MM/DD/YYYY HH:MI:SS AM/PM into DD/MM/YYYY using C# ?I am using C#2008.

Thanks

like image 514
David Avatar asked Sep 04 '25 16:09

David


1 Answers

Use TryParseExact to parse to a DateTime, then ToString with a format string to convert back...

DateTime dt;
if (DateTime.TryParseExact(value, "MM/dd/yyyy hh:mm:ss tt",
                           CultureInfo.InvariantCulture, DateTimeStyles.None,
                           out dt))
{
    string text = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
    // Use text
}
else
{
    // Handle failure
}
like image 56
Jon Skeet Avatar answered Sep 07 '25 16:09

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!