I'm trying to convert a date and time value from a JSON string to a double in C#, but I keep getting the below error.
Below is the code i am trying to debug.
DateTime baseDate = DateTime.SpecifyKind(DateTime.Parse("1970-01-01"), DateTimeKind.Utc);
docRoot.scheduleDate = baseDate.AddSeconds((double)docRoot.scheduleDate);
I understand what the error is saying but I've been having a difficult time finding a workaround. I've tried the approach below but havent succeeded.
docRoot.scheduleDate
to a string, then trying to convert the string to a double using Convert.ToDouble(stringScheduleDate)
. The only issue here is that the special characters in the docRoot.scheduleDate
("10/10/2019 08:29:30") are causing a format error.I believe that if i remove the special characters i'll resolve the issue. But i'm wondering if there is a different approach to getting past this exception?
Converting to a string then removing the special characters seems a bit inefficient in my opinion, so if anyone is willing to contribute some other ideas I much appreciate it.
You should be able to get the Ticks to double very easily:
docRoot.scheduleDate = baseDate.AddSeconds((double)docRoot.scheduleDate.Second);
I just realized this was for an add seconds method, this will work better
Also of note. The OADate internally uses a private InternalTicks property too. So that could also cause issues.
Use DateTime.ToOADate
to get a decimal representation as per https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tooadate and DateTime.FromOADate
to deserialize.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.