-1

I am using Embarcadero's C++Builder 10.2 and a TDateTimePicker control. I have been trying to find a way to set the picker's date to today's date every time I create this Form. So far, I have been unsuccessful. If I try the example given using DateTime.Now, the error message reads "undefined symbol DateTime". My code is in the TForm3::FormCreate(TObject *Sender) event, as I only need this info when this form is created.

When I type in DateTimePicker1-> there is a dropdown box with all of the functions, operators, etc., that are available but none of them seem to be able to get todays date and put it into the format for the control.

It would be appreciated if anyone can help me with figuring this out.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user427921
  • 21
  • 4

1 Answers1

0

In the Form's constructor (DO NOT use the TForm::OnCreate event in C++!), you can assign the return value of the System::Sysutils::Date() function, or the System::TDateTime::CurrentDate() class method, to the TDateTimePicker::Date property:

#include <System.SysUtils.hpp>

__fastcall TForm3::TForm3(TComponent *Owner)
    : TForm(Owner)
{
    DateTimePicker1->Date = System::Sysutils::Date();
    // or:
    // DateTimePicker1->Date = System::TDateTime::CurrentDate();
}

This is clearly demonstrated in Embarcadero's DateUtils (C++) example, which is linked to in the TDateTimePicker documentation.

I don't know what example you are referring to, but there is no DateTime type in C++Builder (or Delphi), that is why you are getting a compiler error. The correct type name is TDateTime instead, but Now() is not a method of the TDateTime class, it is a standalone function in the System::Sysutils namespace instead:

DateTimePicker1->DateTime = System::Sysutils::Now();
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770