-2

I can't figure a way to parse a Datetime object that is "10/24/2022 4:01:35 PM" to "24/10/2022 16:01:35".

Can't find an answer for this exact format.
Thanks in advance!

Rebecca
  • 27
  • 4
  • `ParseExact` + `ToString` ... asked multiple times(I do not have link to duplicate) – Selvin Oct 24 '22 at 13:15
  • 4
    There is no parsing to be done. You parse a string to a DateTime. If you already have a DateTime then there's nothing to parse and it has no format. If you need a string then you call ToString. That's it, that's all. – jmcilhinney Oct 24 '22 at 13:17
  • If what you have is a `DateTime` object and what you want is a string representing that `DateTime` value in a specific format then you're just looking for `ToString()` with format specifiers. It's not really clear to me where you're stuck. Can you provide a specific example of your attempt and indicate what isn't working as expected? – David Oct 24 '22 at 13:17
  • Please show relevant code, as your question is rather unclear. – Charlieface Oct 24 '22 at 13:30

1 Answers1

1

I guess you don't need to parse that string to DateTime since you have it already in Process.StartTime and you're looking at it in the debugger. Then use it directly. If you really have it as string you simply need to use DateTime.Parse with InvariantCulture:

DateTime dt = DateTime.Parse("10/24/2022 4:01:35 PM", System.Globalization.CultureInfo.InvariantCulture);

and for the output:

Console.Write(dt.ToString("dd'/'MM'/'yyyy HH:mm:ss"));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939