Question with DateTime.ToOADate ()

something that I could not understand using DateTime.ToOaDate ().

that's what i have

var plainDate = "01/07/2011"; var dateTime1 = DateTime.Parse(plainDate, new CultureInfo("en-GB")); var value = dateTime1.ToOADate(); var dateTime2 = DateTime.Parse(DateTime.FromOADate(value).ToString(), new CultureInfo("en-GB")); 

dateTime1 and dateTime2 should be the same, right? I only converted a simple date to tics and then restored it as a DateTime with the same culture, but actually when I run it dateTime2 has the value (January 7, 2011) instead of (July 1, 2011)

+6
source share
2 answers

I think this is because your ToString call does not indicate culture information.

Try ToString(new CultureInfo("en-GB")) .

+5
source

When you execute DateTime.Parse , you specify the format in which you expect the incoming date.

When you execute ToString() , you indicate that the current culture should be used to format the date.

Here, I think your Thread.CurrentThread.CurrentCulture.DateTimeFormat returns the US format. Therefore, DateTime.FromOADate(value).ToString() returns on 07/01/2011 , when you parse this with en-GB, it returns on January 7th.

+5
source

Source: https://habr.com/ru/post/914245/


All Articles