-2
COleDateTime m_dt;
m_ctrlDateTime.GetTime(m_dt);
double d = dt.m_dt;
System::DateTime datum; 
datum.FromOADate(d);

I am trying to get date and time from a DateTimePicker control and to later on set the value of datum to that value. Datum is System::DateTime (C#). But datum is this "1/1/1 00:00:00" What's the problem?

halfer
  • 19,824
  • 17
  • 99
  • 186
tamtam
  • 13
  • 5
  • Need something like : DateTime now = m_ctrlDateTime.GetTime(m_dt); – jdweng Jun 19 '16 at 12:11
  • @jdweng How would that work? The `GetTime` member function doesn't return a `DateTime` value. It actually returns a `BOOL`, which means it might even compile, performing an implicit conversion in order to call the `DateTime` constructor that takes a `long`. – Cody Gray - on strike Jun 19 '16 at 12:20
  • Why do you need the line? – jdweng Jun 19 '16 at 12:49

1 Answers1

1

The problem is the very last line:

datum.FromOADate(d);

DateTime::FromOADate is actually a static member function that returns a DateTime object. In C++ terms, you can think of it like a named constructor.

It does not initialize datum like a normal member function would. What's confusing you is the fact that C++ allows you to call static members using an instance of the object. In C#, that would not be possible, and you would have gotten a compile-time error alerting you to the problem.

Write the code like this, and you will be fine:

COleDateTime m_dt;
m_ctrlDateTime.GetTime(m_dt);
double d = dt.m_dt;
System::DateTime datum = System::DateTime::FromOADate(d);

You could also do the following (but it would be similarly confusing):

COleDateTime m_dt;
m_ctrlDateTime.GetTime(m_dt);
double d = dt.m_dt;
System::DateTime datum; 
datum = datum.FromOADate(d);
Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574