Description
The DateTimeToStr
method converts a date/time value to a string according to the user’s locale settings. The date part is converted using the short date format.
To customize the format of a date/time value, for example, get a string containing only the date or time part of the value, use the DateTimeToFormatStr
method.
Declaration
Applies To
The method is applied to the following object:
Parameters
The method has the following parameter:
D
The date/time value to be converted to a string. If the date part of the value is missing, it is assumed to be the “zero date”, December 30, 1899. If the time part is missing, it is not included in the resulting string.
Result Value
A string that contains the specified date and time.
Remarks
If the two-digit year format is used in your Windows operating system, there can be ambiguity in the year presentation. For example, 19 can stand for 1919 and 2019. To avoid confusion, for years of other centuries, this method will always return the year part in the four-digit format.
Example
The following example demonstrates how you can convert date and/or time values to strings.
JavaScript, JScript
function DateTimeToStrSample()
{
var dt;
dt = aqDateTime.SetDateTimeElements(2010, 1, 1, 10, 0, 0); // both date and time
Log.Message(aqConvert.DateTimeToStr(dt));
dt = aqDateTime.SetDateElements(2010, 1, 1); // date only
Log.Message(aqConvert.DateTimeToStr(dt));
dt = aqDateTime.SetTimeElements(10, 0, 0); // time only
Log.Message(aqConvert.DateTimeToStr(dt));
}
/*
The routine produces the following output for the US English locale:
1/1/2010 10:00:00 AM
1/1/2010
12/30/1899 10:00:00 AM
*/
Python
def DateTimeToStrSample():
dt = aqDateTime.SetDateTimeElements(2010, 1, 1, 10, 0, 0) # both date and time
Log.Message(aqConvert.DateTimeToStr(dt))
dt = aqDateTime.SetDateElements(2010, 1, 1) # date only
Log.Message(aqConvert.DateTimeToStr(dt))
dt = aqDateTime.SetTimeElements(10, 0, 0) # time only
Log.Message(aqConvert.DateTimeToStr(dt))
# The routine produces the following output for the US English locale:
# 1/1/2010 10:00:00 AM
# 1/1/2010
# 12/30/1899 10:00:00 AM
VBScript
Sub DateTimeToStrSample
Dim dt
dt = aqDateTime.SetDateTimeElements(2010, 1, 1, 10, 0, 0) ' both date and time
Log.Message aqConvert.DateTimeToStr(dt)
dt = aqDateTime.SetDateElements(2010, 1, 1) ' date only
Log.Message aqConvert.DateTimeToStr(dt)
dt = aqDateTime.SetTimeElements(10, 0, 0) ' time only
Log.Message aqConvert.DateTimeToStr(dt)
End Sub
' The routine produces the following output for the US English locale:
' 1/1/2010 10:00:00 AM
' 1/1/2010
' 12/30/1899 10:00:00 AM
DelphiScript
procedure DateTimeToStrSample;
var dt;
begin
dt := aqDateTime.SetDateTimeElements(2010, 1, 1, 10, 0, 0); // both date and time
Log.Message(aqConvert.DateTimeToStr(dt));
dt := aqDateTime.SetDateElements(2010, 1, 1); // date only
Log.Message(aqConvert.DateTimeToStr(dt));
dt := aqDateTime.SetTimeElements(10, 0, 0); // time only
Log.Message(aqConvert.DateTimeToStr(dt));
end;
{
The routine produces the following output for the US English locale:
1/1/2010 10:00:00 AM
1/1/2010
12/30/1899 10:00:00 AM
}
C++Script, C#Script
function DateTimeToStrSample()
{
var dt;
dt = aqDateTime["SetDateTimeElements"](2010, 1, 1, 10, 0, 0); // both date and time
Log["Message"](aqConvert["DateTimeToStr"](dt));
dt = aqDateTime["SetDateElements"](2010, 1, 1); // date only
Log["Message"](aqConvert["DateTimeToStr"](dt));
dt = aqDateTime["SetTimeElements"](10, 0, 0); // time only
Log["Message"](aqConvert["DateTimeToStr"](dt));
}
/*
The routine produces the following output for the US English locale:
1/1/2010 10:00:00 AM
1/1/2010
12/30/1899 10:00:00 AM
*/
See Also
Working With Dates
Working With Time
aqDateTime Object
aqConvert.DateTimeToFormatStr Method
aqConvert.StrToDateTime Method
aqConvert.StrToDate Method
aqConvert.StrToTime Method