Obtaining DateTimePicker Date and Time in Desktop Windows Applications

Applies to TestComplete 15.47, last modified on January 20, 2023

While testing combo box controls, you can use specific properties and methods of the corresponding program object to perform certain actions and obtain data stored in controls. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with the needed properties and methods from your scripts. However, when testing a control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

The DateTimePicker control allows you to select a date or time. To determine whether the DateTimePicker control is used for date or time selection, use the wIsDateFormat property of the Win32DateTimePicker object. During your test run this object is associated with all DateTimePicker controls whose class names are listed in the project’s Object Mapping options. For more information, see the Getting DateTimePicker Format in Desktop Windows Applications topic.

To obtain the date or time currently selected in the DateTimePicker control, you can use the wDate or wTime property of the Win32DateTimePicker object.

The example below demonstrates how you can determine whether the specified DateTimePicker control is used for date or time selection and get the date or time selected in the control. In this example, we use the DateTimePicker control that is displayed on the fourth page of Windows Scheduled Task Wizard in the Windows XP operating system (this page is shown for the tasks that are supposed to be executed only once):

JavaScript, JScript

function Main()
{
  var DateTimePicker;
  //Obtain the DateTimePicker object
  DateTimePicker = Sys.Process("Explorer").Window("#32770", "Scheduled Task Wizard", 1).Window("#32770", "", 1).Window("SysDateTimePick32", "", 2);

  //Call the TakeDateTime function, that specifies whether the DateTimePicker shows date or time
  //and shows the selected date or time to the user
  TakeDateTime(DateTimePicker);
}

function TakeDateTime(DateTimePicker)
{
  if (DateTimePicker.wIsDateFormat == true)
    {
    // If the wIsDateFormat returns true, the DateTimePicker is used for date selection
    Log.Message("The DateTimePicker control is used for date selection");
    // Return the date currently selected in the DateTimePicker control
    Log.Message("The currently selected date is " + aqConvert.DateTimeToStr(DateTimePicker.wDate))
    }
  else
    {
    // If the wIsDateFormat does not return true, the DateTimePicker is used for time selection
    Log.Message("The DateTimePicker control is used for time selection");
    // Return the time currently selected in the DateTimePicker control
    Log.Message("The currently selected time is " + aqConvert.DateTimeToStr(DateTimePicker.wTime));
    }
}

Python

def Main():
  
  # Obtain the DateTimePicker object
  DateTimePicker = Sys.Process("Explorer").Window("#32770", "Scheduled Task Wizard", 1).Window("#32770", "", 1).Window("SysDateTimePick32", "", 2)

  # Call the TakeDateTime function, that specifies whether the DateTimePicker shows date or time
  # and shows the selected date or time to the user
  TakeDateTime(DateTimePicker)

def TakeDateTime(DateTimePicker):
  if DateTimePicker.wIsDateFormat:
    # If the wIsDateFormat returns True, the DateTimePicker is used for date selection
    Log.Message("The DateTimePicker control is used for date selection")
    # Return the date currently selected in the DateTimePicker control
    Log.Message("The currently selected date is " + str(DateTimePicker.wDate))
  else:
    # If the wIsDateFormat does not return True, the DateTimePicker is used for time selection
    Log.Message("The DateTimePicker control is used for time selection")
    # Return the time currently selected in the DateTimePicker control
    Log.Message("The currently selected time is " + str(DateTimePicker.wTime))

VBScript

Sub Main
Dim DateTimePicker
  'Obtain the DateTimePicker object
  Set DateTimePicker = Sys.Process("Explorer").Window("#32770", "Scheduled Task Wizard", 1).Window("#32770", "", 1).Window("SysDateTimePick32", "", 2)

  'Call the TakeDateTime function, that specifies whether the DateTimePicker shows date or time
  'and shows the selected date or time to the user
  Call TakeDateTime(DateTimePicker)
End Sub

Sub TakeDateTime(DateTimePicker)
  If DateTimePicker.wIsDateFormat = true then
    'If the wIsDateFormat returns true, the DateTimePicker is used for date selection
    Call Log.Message("The DateTimePicker control is used for date selection")
    'Return the date currently selected in the DateTimePicker control
    Call Log.Message("The currently selected date is " + aqConvert.DateTimeToStr(DateTimePicker.wDate))
  Else
    'If the wIsDateFormat does not return true, the DateTimePicker is used for time selection
    Call Log.Message("The DateTimePicker control is used for time selection")
    'Return the time currently selected in the DateTimePicker control
    Call Log.Message("The currently selected time is " + aqConvert.DateTimeToStr(DateTimePicker.wTime))
  End If
End Sub

DelphiScript

procedure TakeDateTime(DateTimePicker);
begin
  if DateTimePicker.wIsDateFormat = true then
    begin
    // If the wIsDateFormat returns true, the DateTimePicker is used for date selection
    Log.Message('The DateTimePicker control is used for date selection');
    // Return the date currently selected in the DateTimePicker control
    Log.Message('The currently selected date is ' + aqConvert.DateTimeToStr(DateTimePicker.wDate));
    end
  else
    begin
    // If the wIsDateFormat does not return true, the DateTimePicker is used for time selection
    Log.Message('The DateTimePicker control is used for time selection');
    // Return the time currently selected in the DateTimePicker control
    Log.Message('The currently selected time is ' + aqConvert.DateTimeToStr(DateTimePicker.wTime));
    end;
end;

procedure Main();
var DateTimePicker;
begin
  // Obtain the DateTimePicker object
  DateTimePicker := Sys.Process('Explorer').Window('#32770', 'Scheduled Task Wizard', 1).Window('#32770', '', 1).Window('SysDateTimePick32', '', 2);

  // Call the TakeDateTime function, that specifies whether the DateTimePicker shows date or time
  // and shows the selected date or time to the user
  TakeDateTime(DateTimePicker);
end;

C++Script, C#Script

function Main()
{
  var DateTimePicker;
  // Obtain the DateTimePicker object
  DateTimePicker = Sys["Process"]("Explorer")["Window"]("#32770", "Scheduled Task Wizard", 1)["Window"]("#32770", "", 1)["Window"]("SysDateTimePick32", "", 2);

  // Call the TakeDateTime function, that specifies whether the DateTimePicker shows date or time
  // and shows the selected date or time to the user
  TakeDateTime(DateTimePicker);
}

function TakeDateTime(DateTimePicker)
{
  if (DateTimePicker["wIsDateFormat"] == true)
    {
    // If the wIsDateFormat returns true, the DateTimePicker is used for date selection
    Log.Message("The DateTimePicker control is used for date selection");
    // Return the date currently selected in the DateTimePicker control
    Log.Message("The currently selected date is " + aqConvert["DateTimeToStr"](DateTimePicker["wDate"]));
    }
  else
    {
    // If the wIsDateFormat does not return true, the DateTimePicker is used for time selection
    Log.Message("The DateTimePicker control is used for time selection");
    // Return the time currently selected in the DateTimePicker control
    Log.Message("The currently selected time is " + aqConvert["DateTimeToStr"](DateTimePicker["wTime"]));
    }
}

See Also

Working With DateTimePicker Controls in Desktop Windows Applications
wDate Property (DateTimePicker Controls)
wTime Property (DateTimePicker Controls)

Highlight search results