7

How can convert a datetime to double?

Siva
  • 561
  • 3
  • 7
  • 14
  • out of curiosity, why do you need/want to do this? If it's for datetime arithmetic you should just use the static API of the DateTime class. – Brian Driscoll Mar 03 '11 at 04:37
  • 1
    What format are you going for with this? Seems a bit odd. How do you want to represent the date in your floating point number? EDIT: Ninja'd – Ed S. Mar 03 '11 at 04:37
  • 5
    What exactly would the double represent? – user541686 Mar 03 '11 at 04:37
  • Like any other sort of date accumulator, a double as a date represents the number of units (nanoseconds, milliseconds, etc..) since some epoch. – Ritch Melton Mar 03 '11 at 04:43
  • @Ritch - what epoch and what units is exactly what @Mehrdad's asking. Without knowing that it's hard to give an answer. I'll note that your answer doesn't really fit this definition since the integral portion and fractional portion have different units in an OADate. – tvanfosson Mar 03 '11 at 04:47
  • 1
    @tvanfosson I disagree. The OP wanted a double. I gave him a formatted double. I think there's some concern with the original intent of the function (ie: to support OLE Automation), but if the consumer of the double uses the documented values, then there's no problem in re-using that function. Now, I do doubt that the OP _actually_ needs a double, and could use the object directly as is. – Ritch Melton Mar 03 '11 at 04:53
  • @Ritch - I just meant that an OADate doesn't represent *the number of units...since some epoch*. Strictly speaking the integral portion represents the number of days since the epoch while the fractional portion represents the percentage of hours in the day since midnight. In some ways it can be handier for a particular purpose (the math is simpler to find the time of day, for example), but you can't, for instance, convert to different units easily since it doesn't represent a single unit measure. – tvanfosson Mar 03 '11 at 05:02
  • @tvanfosson Oh, that clarifies things. I understand the issue you are raising. – Ritch Melton Mar 03 '11 at 05:12

2 Answers2

13
var converted = DateTime.Now.ToOADate();
Ritch Melton
  • 11,498
  • 4
  • 41
  • 54
  • 2
    Good answer to the question "How do I convert a DateTime into a date usable for OLE Automation?" Since we don't know if that's what's needed for the OP, it's hard to up vote for this question. – tvanfosson Mar 03 '11 at 04:43
  • @RitchMelton Good Answer I had not known the `ToOADate()` option in DateTime before – Harsh Baid Mar 03 '11 at 04:43
  • Its a double. If he uses the same epoch and counter amount, then it does what he needs. That information is in the help docs, linky: http://msdn.microsoft.com/en-us/library/system.datetime.tooadate.aspx – Ritch Melton Mar 03 '11 at 04:45
  • I do concur with the why do you need this/what does it represent questions though. – Ritch Melton Mar 03 '11 at 04:46
  • @tvanfosson what does `OP` means – Harsh Baid Mar 03 '11 at 04:46
  • @Harsh - OP == Original Poster, in this case the person who asked the question. – tvanfosson Mar 03 '11 at 04:47
1

A better approach than ToOADate is to use this:

double timestamp = DateTime.UtcNow.Ticks;

See this post where precision is lost when doing DateTime.Now.AddTicks(1000).ToOADate(), if the example there were changed to (double)DateTime.Now.AddTicks(1000).Ticks the precision is not lost.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
BornToCode
  • 9,495
  • 9
  • 66
  • 83