-1
var dateString = "23/12/2019 06:30:00";
DateTime dt = DateTime.ParseExact(recordDateString.Trim(), "dd/MM/yyyy HH:mm:ss", new CultureInfo("fr-FR"));

I expect this output : 23/12/2019 06:30:00 But dt object value is : 12/23/2019 06:30 PM

Where is wrong here?

Note: The code is written in .net core 2.2

xangkcl
  • 57
  • 1
  • 7
  • Where do you look at this output? What tools are you using to see it? What line of code produces that output? – Steve Dec 25 '20 at 20:28
  • I'm using visual studio 2019, I see this output debug mode when my project run locally. – xangkcl Dec 25 '20 at 21:27
  • So your Visual Studio uses a culture where the date are presented to you in that way. You have nothing to worry, it is just how dates work. Internally they are just numbers and when you need to show a date, a tool comes into play that 'formats' those numbers to display a date following the culture of the current machine. (Or any other culture if you specify the culture you require) – Steve Dec 25 '20 at 21:51
  • And because dates are internally numbers you can subtract a date from another date getting back a TimeSpan simply writing _TimeSpan ts = dt1 - dt2;_ – Steve Dec 25 '20 at 21:53
  • Thanks a lot @Steve, although the datetime object dt1 format MM/dd/yyyy and dt2 object format dd/MM/yyyy, dt1-dt2 result is correct. – xangkcl Dec 28 '20 at 08:08
  • Keep in mind. Dates have no format! It is the tools that display the date that formats the number in something that you interpret as a date – Steve Dec 28 '20 at 09:39

1 Answers1

1

Your dt variable contains what you expect - December 23rd, 2019. But when you print it, it is probably printed with the en-US locale, which uses MM/dd/yyyy. The DateTime type only stores the actual time value, not the locale (so it kinda forgets that you parsed it from fr-FR), it can be parsed from any locale and printed to any other locale. Try dt.ToString(new CultureInfo("fr-FR")).

fejesjoco
  • 11,763
  • 3
  • 35
  • 65
  • dt.ToString(new CultureInfo("fr-FR")) return string, but I need datetime object with dd/MM/yyyy. Actually I want to do substraction between two date object. One of these format MM/dd/yyyy, another date object format dd/MM/yyyy @fejesjoco – xangkcl Dec 25 '20 at 21:24
  • "but I need datetime object" -> that's what you have in the dt variable "with dd/MM/yyyy" -> that only matters when you convert the date to a string – fejesjoco Dec 26 '20 at 10:54