Skip to content

DateSet

See Also: Time and Date Functions

Purpose

The DateSet function returns a new DateTime value comprised of the passed year, month, and day.

This can be used in place of DateSetYear, DateSetMonth, and DateSetDay. It is simpler to use and it tests for a valid date.

Return Type

DateTime

Syntax

Use GlobalDateTimeFunctions.pkg
DateSet( {iYear}, {iMonth}, {iDay} )

Parameters

  • {iYear}: An integer value representing the new year component of the DateTime.
  • {iMonth}: An integer value representing the new month component of the DateTime.
  • {iDay}: An integer value representing the new day component of the DateTime.

Example

Procedure Test
    DateTime dtVar
    Move (DateSet(2015, 9, 26)) to dtVar
    // This will print (using US date settings):
    // 9/26/2015 12:00:00 AM
    Showln dtVar
End_Procedure

Before returning the variable, the validity of the date is tested. If invalid, a DFERR_INVALID_DATETIME is raised.

Example of Invalid Date

Procedure Test
    DateTime dtVar
    Move (DateSet(2015, 19, 26)) to dtVar
End_Procedure

The code above will trigger error 4523 "DateTime contains an invalid value" because month 19 does not exist.

A null date (year=0, month=0, day=0) is considered to be an invalid date. If you wish to define a null date, use NullDateTime.

Notes

  • The DateTime data type can hold invalid DateTime values, such as 02/31/2025. However, when a valid DateTime is required, such as with DateTime arithmetic, error 4523 "The specified DateTime contains an invalid value" will be raised and the result will be a Null DateTime/TimeSpan.

  • The DateTime data type uses the Windows Regional Settings for the date and time format when converting to/from string. The time format specified in the Windows Regional Settings does not support milliseconds. Therefore, for the DateTime type, the format for milliseconds is expected to be in fractions of seconds, using the decimal specifier. For example, 1/15/2007 3:03:10.545, where it's 10 seconds and 545 milliseconds, expressed in fractions as 10.545 seconds. Trailing zeroes are normally stripped in a manner consistent with fractional parts, so 10.54 seconds is naturally the same as 10.540 seconds.

  • The DateTime data type can hold invalid DateTime values. Conversion to/from string can be performed on invalid DateTime values without changing the value. For example, Move "11/31/2006" to dtValue is OK. However, when a valid DateTime is required, such as with DateTime arithmetic, error 4523 "The specified DateTime contains an invalid value" will be raised and the result will be a Null DateTime/TimeSpan.