aqConvert.DateTimeToFormatStr Method

Applies to TestComplete 15.44, last modified on November 10, 2022

Description

The DateTimeToFormatStr method converts a Date value to a string whose format is defined by the FormatStr parameter.

Declaration

aqConvert.DateTimeToFormatStr(D, FormatStr)

D [in]    Required    Date/Time    
FormatStr [in]    Required    String    
Result String

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

D

Specifies the value that contains the desired date and time.

FormatStr

Specifies the format that should be used for the conversion. For more information on format specifiers, see Date and Time Format Specifiers.

Result Value

The resulting string that contains the given date and time in the specified format.

Example

The following example demonstrates how to use the DateTimeToFormatStr method:

JavaScript, JScript

function DateTimeToFormatStrSample()
{
  var dt;

  dt = aqDateTime.SetDateTimeElements(2010, 1, 1, 10, 0, 0); // both date and time
  Log.Message(aqConvert.DateTimeToFormatStr(dt, "%m/%d/%Y %H:%M"));

  dt = aqDateTime.SetDateElements(2010, 1, 1); // date only
  Log.Message(aqConvert.DateTimeToFormatStr(dt, "%m/%d/%Y"));

  dt = aqDateTime.SetTimeElements(10, 0, 0); // time only
  Log.Message(aqConvert.DateTimeToFormatStr(dt, "%H:%M"));
}

/*
The routine produces the following output:

01/01/2010 10:00
01/01/2010
10:00
*/

Python

def DateTimeToFormatStrSample():
  dt = aqDateTime.SetDateTimeElements(2010, 1, 1, 10, 0, 0) # both date and time 
  Log.Message(aqConvert.DateTimeToFormatStr(dt, "%m/%d/%Y %H:%M"))
  dt = aqDateTime.SetDateElements(2010, 1, 1) # date only 
  Log.Message(aqConvert.DateTimeToFormatStr(dt, "%m/%d/%Y"))
  dt = aqDateTime.SetTimeElements(10, 0, 0) # time only 
  Log.Message(aqConvert.DateTimeToFormatStr(dt, "%H:%M"))

# The routine produces the following output: 
# 01/01/2010 10:00 
# 01/01/2010 
# 10:00

VBScript

Sub DateTimeToFormatStrSample
  Dim dt

  dt = aqDateTime.SetDateTimeElements(2010, 1, 1, 10, 0, 0) ' both date and time
  Log.Message aqConvert.DateTimeToFormatStr(dt, "%m/%d/%Y %H:%M")

  dt = aqDateTime.SetDateElements(2010, 1, 1) ' date only
  Log.Message aqConvert.DateTimeToFormatStr(dt, "%m/%d/%Y")

  dt = aqDateTime.SetTimeElements(10, 0, 0) ' time only
  Log.Message aqConvert.DateTimeToFormatStr(dt, "%H:%M")
End Sub

' The routine produces the following output:

' 01/01/2010 10:00
' 01/01/2010
' 10:00

DelphiScript

procedure DateTimeToStrFormatSample;
var dt;
begin
  dt := aqDateTime.SetDateTimeElements(2010, 1, 1, 10, 0, 0); // both date and time
  Log.Message(aqConvert.DateTimeToFormatStr(dt, '%m/%d/%Y %H:%M'));

  dt := aqDateTime.SetDateElements(2010, 1, 1); // date only
  Log.Message(aqConvert.DateTimeToFormatStr(dt, '%m/%d/%Y'));

  dt := aqDateTime.SetTimeElements(10, 0, 0); // time only
  Log.Message(aqConvert.DateTimeToFormatStr(dt));
end;

{
The routine produces the following output:

01/01/2010 10:00
01/01/2010
10:00
}

C++Script, C#Script

function DateTimeToStrFormatSample()
{
  var dt;

  dt = aqDateTime["SetDateTimeElements"](2010, 1, 1, 10, 0, 0); // both date and time
  Log["Message"](aqConvert["DateTimeToFormatStr"](dt, "%m/%d/%Y %H:%M"));

  dt = aqDateTime["SetDateElements"](2010, 1, 1); // date only
  Log["Message"](aqConvert["DateTimeToFormatStr"](dt, "%m/%d/%Y"));

  dt = aqDateTime["SetTimeElements"](10, 0, 0); // time only
  Log["Message"](aqConvert["DateTimeToFormatStr"](dt, "%H:%M"));
}

/*
The routine produces the following output:

01/01/2010 10:00
01/01/2010
10:00
*/

See Also

Working With Dates
Working With Time
Date and Time Format Specifiers
aqDateTime Object
DateTimeToStr Method
StrToDateTime Method
StrToDate Method
StrToTime Method

Highlight search results