OScript API/Built-in Package Index

Class: Date

The Date package supplies a set of built-ins for querying the current time and date, and creating and manipulating Date values. The major functionalities offered are the following:

Class Methods

Clock( )

Returns the number of clock tick since the start of the process.

Date( Date theDate )

Returns a copy of theDate with the time portion cleared to 00:00:00.

DateInteger( Date theDate, [Integer nDays] )

Converts a Date to an Integer or an Integer to a Date.

DateToList( Date theDate )

Breaks a Date into a List of numerical date elements.

DateToString( Date theDate, [String format] )

Returns a String representation of a Date.

Day( Date theDate )

Returns the day of the month for a specified Date.

DayofWeek( Date theDate )

Returns the day of the week for a specified Date.

DayofYear( Date theDate )

Returns the year for a specified Date.

Hour( Date theDate )

Returns the number of hours past midnight for a specified Date.

ListToDate( List theList )

Creates a Date from a List of numerical date elements.

Returns the elapsed time in microseconds; useful for calculating timing.

Minute( Date theDate )

Returns the number of minutes past the hour for a specified Date.

Month( Date theDate )

Returns the numerical month for a specified Date.

NextCronTime( Date theDate, String cronString )

Calculates a Date relative to the specified Date, as specified by a cron-like string.

NextTime( Date theDate, Integer MinsOfHour, Integer HoursOfDay, Integer DaysOfWeek, Integer MonthsOfYear )

Calculates a Date relative to the specified Date, as specified by a set of bit masks.

Now( )

Returns a Date for the current date and time.

Second( Date theDate )

Returns the number of seconds past the minute for a given Date.

StringToDate( String dateString, String format )

Creates a Date from a String representation.

Returns the current value of the server system clock.

Tick( )

Returns the elapsed time in milliseconds; useful for calculating timing.

Time( Date theDate )

Returns a copy of theDate with the date portion cleared.

TimeInteger( Date theDate, [Integer nSeconds] )

Converts the time portion of a Date value from a Date to an Integer or from an Integer to a Date.

TimeReal( Date theDate, [Real nSeconds] )

Converts the time portion of a Date value from a Date to a Real value

UTNow( )

Returns the current Greenwich Mean time.

Year( Date theDate )

Returns the year from a given Date.

Class Methods

Clock

Integer Clock()

Returns Returns the number of clock tick since the start of the process.

Returns:

The number of clock tick since the start of the process.

Date

Date Date( Date theDate )

Returns a copy of the specified Date, clearing the time in the copy to 00:00:00. See Time().

The output from a test is:

Parameters

theDate

Specifies a Date for which a time-cleared copy will be returned.

Returns:

A new date whose time information is set to 00:00:00.

Example

This simple example demonstrates how Date() can be used to isolate the date portion of a Date value:

Date    rightNow = Date.Now()
Date    todaysDate = Date.Date( rightNow )

Echo( "The date is ", todaysDate )

The output from a test is:

The date is Thu Aug 06 00:00:00 1998

DateInteger

Dynamic DateInteger( Date theDate,
                     [Integer nDays] )

Converts the date portion of a Date value to or from an Integer number of days relative to October 15, 1582 (the start of the Gregorian Calendar). To convert the time portion of a Date value, see TimeInteger().

Parameters

theDate

If nDays is not specified, theDate is converted to an Integer number of days relative to October 15, 1582. Otherwise, if nDays is specified, the time portion of theDate will fill in the resulting Date converted from nDays.

nDays

An Integer number of days since October 15, 1582, which, if specified, is converted to the date portion of a new Date value.

Returns:

If nDays is not specified, an Integer conversion of the date portion of theDate into a number of days relative to October 15, 1582. Otherwise, it returns a new Date value converted from nDays and the time portion of theDate.

Example

Note that negative integers are handled as well, referring to a given number of days before October 15, 1582.

This example uses DateInteger() to calculate a relative date:

Echo( "Today is ", Date.Now() )
Echo( "Five days from now is ", calculateLaterDate( Date.Now(), 5 ) )

function Date CalculateLaterDate( Date theDate, Integer moreDays )
    Integer dateDays = Date.DateInteger( theDate )
    Integer laterDays = dateDays + moreDays
    Date    laterDate = Date.DateInteger( theDate, laterDays )

    return laterDate
end

The output of this example is:

Today is Tue Aug 11 10:44:49 1998
Five days from now is Sun Aug 16 10:44:49 1998

DateToList

List DateToList( Date theDate )

Converts a Date to a List with six individual date elements. See ListToDate().

The result from one test is:

Parameters

theDate

Specifies a Date to convert to a List.

Returns:

A List containing the following integer elements:

Example

This example uses the DateToList function which converts the Date to the List:

Date    rightNow = Date.Now()
List    dateList = Date.DateToList( rightNow )
String  hours = Str.String( dateList [ 4 ] )
String  minutes = Str.String( dateList [ 5 ] )

Echo( 'It is ' + hours + ' hours and ' + minutes + ' minutes past midnight.' )

The result from one test is:

It is 11 hours and 51 minutes past midnight.

DateToString

String DateToString( Date theDate,
                     [String format] )

Converts a Date to a String. The optional format String allows for a high degree of customization in the resulting String representation. See StringToDate().

Parameters

theDate

Specifies a Date to be converted.

format

Specifies an String indicating how the resulting String should be formatted. It can contain both text, and any of the following format characters:

Value Description
%% A percentage sign
%a The three-character abbreviated weekday name (e.g., Mon, Tue, etc.)
%b The three-character abbreviated month name (e.g., Jan, Feb, etc.)
%d The two-digit day of the month, from 01 to 31 (e.g., 01-31)
%j The three-digit day of year, from 001 through 366
%m The two-digit month (e.g., 01-12)
%p AM or PM
%w The 1-digit weekday, from 1 through 7, where 1= Sunday
%y The two-digit year (e.g., 93)
%A The full weekday name (e.g., Monday)
%B The full month name (e.g., January)
%H The two-digit hour on a 24-hour clock, from 00 to 23
%I The two-digit hour, from 01 through 12
%M The minutes past the hour, from 00 to 59
%S The seconds past the minute, from 00 to 59
%L The three-digit milliseconds, from 000 to 999
%Y The year, including the century (e.g., 1993)
%P AD or BC

Returns:

If a valid Date is specified, a String representation of the Date is returned, 0 otherwise. If no format String is specified, the default format String is used, namely "%a %b %d %H:%M:%S %Y".

Example

This example uses DateToString() to convert a Date to a custom String representation:

Date rightNow = Date.Now()

Echo( Date.DateToString( rightNow, "It is now %H:%M on %A, %B %d." ) )

The output of this example is:

It is now 10:51 on Tuesday, August 11.

Day

Integer Day( Date theDate )

Returns the day of the month for a specified Date.

Parameters

theDate

Specifies a Date for which the day of the month is returned.

Returns:

An Integer, from 1 to 31, representing the day of the month for the given Date.

Example

This example uses the Day() function to return the day of the month from the given Date:

Integer daysLeft
Integer dayOfMonth = Date.Day( Date.Now() )

if dayOfMonth <= 15
   daysLeft = ( 15 - dayOfMonth )
else
   daysLeft = ( 31 - dayOfMonth )
end

Echo( "There are ", daysLeft, " days til payday!" )

The output of this example is:

There are 4 days til payday!

DayofWeek

Integer DayofWeek( Date theDate )

Returns the day of the week for a specified Date.

Parameters

theDate

Specifies a Date for which the day of the week is returned.

Returns:

An Integer, from 1 to 7, representing the day of the week, where 1 = Sunday.

Example

This example uses the DayOfWeek() function to return the day of the week from a given Date:

Date    rightNow = Date.Now()
Integer dayOfWeek = Date.DayOfWeek( rightNow )
String  todayString = Date.DateToString( rightNow, "%A" )

if ( dayOfWeek == 0 || dayOfWeek == 7 )
    Echo( todayString, " is the weekend!" )
else
    Echo( todayString, " is a weekday." )
end

The output of this example is:

Tuesday is a weekday.

DayofYear

Integer DayofYear( Date theDate )

Returns the day of the year for a specified Date.

Parameters

theDate

Specifies a Date for which the day of the year is returned.

Returns:

An Integer, from 1 to 366, representing the number of days since January 1, where January 1 = 1.

Example

This example uses the DateOfYear() function to return the day of the year from a given Date:

Integer dayOfYear = Date.DayOfYear( Date.Now() )
Date    taxDate = Date.StringToDate( "April 15", "%B %d" )
Integer tilTaxes = Date.DayOfYear( taxDate ) - dayOfYear

if tilTaxes < 0
   tilTaxes = ( tilTaxes + 365 )
end

Echo( "Taxes are due in only ", tilTaxes, " days!" )

The output of this example is:

Taxes are due in only 247 days!

Hour

Integer Hour( Date theDate )

Returns the number of hours past midnight for a specified Date.

Parameters

theDate

Specifies a Date for which the number of hours past midnight is returned.

Returns:

An Integer, from 0 to 23, representing the number of hours past midnight for the given Date.

Example

The value returned is not rounded and represents the number of whole hours that have passed. For instance, Date.Hour() on a Date with a time of 4:59 PM would return 16.

ListToDate

Date ListToDate( List theList )

Converts a List of numerical date elements to a new Date.

Parameters

theList

Specifies a List to convert to a Date.

It must contain the following Integer elements:

Value Description
1 year
2 month (integer from 1 to 12; January = 1)
3 day of the month (integer from 1 to 31)
4 hours since midnight (integer from 0 to 23)
5 minutes past the hour (integer from 0 to 59)
6 seconds past the minute (integer from 0 to 59)

Returns:

A Date if the elements in the given List specify valid values (i.e., not out of the specified range), Undefined otherwise.

Example

This example uses the ListToDate function which converts the List to Date:

List dateList = { 1976, 7, 4, 0, 0, 0 }
Date USDate = Date.ListToDate( dateList )
String stringDate = Date.DateToString( USDate, "%B %d, %Y" )

Echo( "Independence day for the US was " + stringDate + "." )

The output of this example is:

Independence day for the US was July 04, 1976.

MicroTick

Integer MicroTick()

Returns an Integer elapsed time in microseconds. MicroTick() is most often used for timing operations, possibly for system logging or performance evaluation. See Tick() and Systime().

For legacy reasons, the time returned is a 32-bit value, which means that MicroTick()'s value will roll over every 2147 seconds, or approximately 36 minutes. Any code using MicroTick() should be prepared for potential roll-overs.

Returns:

An Integer elapsed time in microseconds.

Example

This function is most often used to compare two elapsed time values, allowing a relatively fine granularity of timing between Date.MicroTick() calls to be determined.

This example uses MicroTick() for simple performance timing:

Integer i
Integer start = Date.MicroTick()

for ( i = 0; i < 100000; i += 1 )
end

Integer endTime = Date.MicroTick()
Integer duration = endTime > start ? endTime - start : endTime + (1 << 31) - start
Echo( "It took ", duration, "µs to count to a hundred thousand in Oscript." )

The output of this example is:

It took 250790µs to count to a hundred thousand in Oscript.

Minute

Integer Minute( Date theDate )

Returns the number of minutes past the hour from a given Date.

Parameters

theDate

Specifies a Date for which the minute past the hour is returned.

Returns:

An Integer, from 0 to 59, representing the number of minutes past the hour for the specified Date.

Month

Integer Month( Date theDate )

Returns the index of the month for a specified Date.

Parameters

theDate

Specifies a Date for which the month is returned.

Returns:

An Integer, from 1 to 12, representing the month index, where January = 1.

Example

This example uses Month() to return the index of the month:

Integer month = Date.Month( Date.Now() )
Integer seasonIndex = ( month / 3 ) + 1
List    seasons = { "winter", "spring", "summer", "fall" }
String  monthString = Date.DateToString( Date.Now(), "%B" )

Echo( monthString, " is in  ", seasons[ seasonIndex ], "." )

The output of this example is:

August is in summer.

NextCronTime

Date NextCronTime( Date theDate,
                   String cronString )

Calculates a relative date from a Date based on a cron-like string. The crontabStr is similar to the a standard UN#X crontab string.

This method works like NextTime().

Parameters

theDate

The initial date.

cronString

It is similar to the a standard UN#X crontab string. It consists of 5 fields separated by spaces or tabs of integer patterns that specify the following:

  • Minute (0-59),
  • Hour (0-23),
  • Day of month (1-31),
  • Month of year (1-12),
  • Day of week (0-6 with 0=Sunday)

Each of these patterns may be either an asterisk (meaning all legal values) or a list of elements separated by commas. An element is either a number or two numbers separated by a minus sign (meaning an inclusive range).

Unlike the UN#X crontab, we only use a 5 minute resolution. So minute values that are not increments of 5 will be rounded down to the previous increment of 5.

Returns:

Returns the next date.

NextTime

Date NextTime( Date theDate,
               Integer MinsOfHour,
               Integer HoursOfDay,
               Integer DaysOfWeek,
               Integer MonthsOfYear )

Calculates a relative date from a Date based on a set of relative time and date mask information. Note that this does no simply calculate a relative date based on integer offsets. Instead, the use of masks allows certain information in the Date to be preserved, while other information can be set to vary at a certain granularity. For simply calculating relative dates, see DateInteger() and TimeInteger().

Parameters

theDate

The initial date.

MinsOfHour

Specifies minutes of hour mask using bits (0-11), 5 min. resolution.

HoursOfDay

Specifies hours of day mask using bits (0-23).

DaysOfWeek

Specifies days of week mask using bits (0-6) or (0-31).

MonthsOfYear

Specifies months of year mask using bits (0-11). If this parameter is set then DaysOfWeek parameter must utilize mask using (0-31).

Returns:

Returns the next date based upon the set of masks.

Example

This example uses NextTime() to calculate relative dates:

Integer i
Integer allDays = 1
Integer allHours = 1 << ( 24 - 1 )
Integer allMinutes = 1 << ( 1 - 1 )
Integer allMonths = 0
Date    d = Date.Now()

// The output is the next four Sundays.
for i = 1 to 4
   echo( d = Date.NextTime( d, allMinutes, allHours, allDays, allMonths ) )
end

The output from a test is:

Sun Aug 16 23:00:00 1998
Sun Aug 23 23:00:00 1998
Sun Aug 30 23:00:00 1998
Sun Sep 06 23:00:00 1998

Now

Date Now()

Returns the current date and time.

Returns:

A Date representing the current server system date and time, if successful.

Example

Now() is the simple and oft-used function for querying the current date and time, as seen in this example:

Echo( "The current date and time is ", Date.Now() )

The output of this example is:

The current date and time is Wed Aug 12 00:55:38 1998

Second

Integer Second( Date theDate )

Returns the number of seconds past the minute for a given Date.

Parameters

theDate

Specifies a Date for which the number of seconds past the minute is returned..

Returns:

An Integer, from 0 to 59, representing the number of seconds past the minute for the given Date.

StringToDate

Date StringToDate( String dateString,
                   String format )

Converts a String representation of a Date to a Date, using a format string to specify the translation. See DateToString().

Parameters

dateString

Specifies a String to be converted.

format

Specifies a String indicating how to interpret theString. It can contain both text and any of the following format characters:

Value Description
%% A percentage sign
%b The three-character abbreviated month name (e.g., Jan, Feb, etc.)
%d The two-digit day of the month, from 01 to 31 (e.g., 01-31)
%m The two-digit month (e.g., 01-12)
%p AM or PM
%y The two-digit year (e.g., 93)
%B The full month name (e.g., January)
%H The two-digit hour on a 24-hour clock, from 00 to 23
%I The two-digit hour, from 01 through 12
%M The minutes past the hour, from 00 to 59
%S The seconds past the minute, from 00 to 59
%L The three-digit milliseconds, from 000 to 999
%Y The year, including the century (e.g., 1993)
%P AD or BC

Returns:

A Date if the given arguments specify a valid Date and format, otherwise Undefined.

Example

This example uses the DateToString function which converts the String to Date:

Date theDate

String dateString = "January 05, 1993"

theDate = Date.StringToDate( dateString, "%B %d, %Y" )

Systime

Integer Systime()

Returns the current value of the server system clock, timed in seconds.

Returns:

An Integer indicating the current value of the server system clock, in seconds.

Example

This function is most often used to compare two system clock values to calculate timing in seconds. See Tick().

Tick

Integer Tick()

Returns an Integer elapsed time in milliseconds. Tick() is most often used for timing operations, possibly for system logging or performance evaluation. See Systime().

Returns:

An Integer elapsed time in milliseconds.

Example

This function is most often used to compare two elapsed time values, allowing a relatively fine granularity of timing between Date.Tick() calls to be determined.

This example uses Tick() for simple performance timing:

Integer i
Integer start = Date.Tick()

for ( i = 0; i < 100000; i += 1 )
end

Echo( "It took ", Date.Tick() - start, "ms to count to a hundred thousand in Oscript." )

The output of this example is:

It took 246ms to count to a hundred thousand in Oscript.

Time

Date Time( Date theDate )

Returns a copy of the specified Date, clearing the date portion of the copy. See Date().

Parameters

theDate

Specifies a Date for which a date-cleared copy will be returned.

Returns:

A Date where the time information remains unaltered and the Date information is set to the first representable day (January 1, 0001).

Example

This example uses Time() to isolate the Time portion of a Date value:

Date    theTime = Date.Time( Date.Now() )
String  timeString = Date.DateToString( theTime, "%M minutes past %I %p" )

Echo( "The time is now " + timeString + "." )

The output of this example is:

The time is now 48 minutes past 11 AM.

TimeInteger

Dynamic TimeInteger( Date theDate,
                     [Integer nSeconds] )

Converts the time portion of a Date value to or from an Integer number of seconds since midnight. To convert the date portion of a Date value, see DateInteger().

Parameters

theDate

If nSeconds is not specified, the time portion of theDate is converted to an Integer number of seconds since midnight. Otherwise, if nSeconds is specified, a new Date value is returned with the date portion taken from theDate and time from nSeconds.

nSeconds

An Integer number of seconds since midnight, which, if specified, is converted to the time portion of a new Date value.

Returns:

If nSeconds is not specified, an Integer conversion of the time portion of theDate into a number seconds since midnight. Otherwise, it returns a new Date value converted from nSeconds and the date portion of theDate.

Example

This example uses TimeInteger() to convert the time portion a Date value into an Integer number of seconds:

Integer nSeconds = Date.TimeInteger( Date.Now() )
Integer lunchSeconds = ( 12 * 60 * 60 ) - nSeconds

if ( lunchSeconds < 0 )
    Echo( "It is ", -lunchSeconds / 60 / 60, " hours after lunch." )
else
    Echo( "It is ", lunchSeconds / 60 / 60, " hours before lunch." )
end

The output of this example for a random test time is:

It is 2 hours after lunch.

TimeReal

Dynamic TimeReal( Date theDate,
                  [Real nSeconds] )

Converts the time portion of a Date value to or from an Real number of seconds since midnight.

Parameters

theDate

If nSeconds is not specified, the time portion of theDate is converted to an Real number of seconds since midnight. Otherwise, if nSeconds is specified, a new Date value is returned with the date portion taken from theDate and time from nSeconds.

nSeconds

A Real number of seconds since midnight, which, if specified, is converted to the time portion of a new Date value.

Returns:

If nSeconds is not specified, an Real conversion of the time portion of theDate into a number seconds since midnight. Otherwise, it returns a new Date value converted from nSeconds and the date portion of theDate.

Example

This example uses TimeReal() to convert the time portion a Date value into an Real number of seconds:

Real    nSeconds = Date.TimeReal( Date.Now() )
Real    lunchSeconds = ( 12.5 * 60 * 60 ) - nSeconds

if ( lunchSeconds < 0 )
    Echo( "It is ", -lunchSeconds / 60 / 60, " hours after lunch." )
else
    Echo( "It is ", lunchSeconds / 60 / 60, " hours before lunch." )
end

The output of this example for a random test time is:

It is 1.660555555556 hours after lunch.

UTNow

Date UTNow()

Returns the current Greenwich Mean time.

Returns:

The current Greenwich Mean time.

Year

Integer Year( Date theDate )

Returns the year for a specified Date.

Parameters

theDate

Specifies a Date for which the year is returned.

Returns:

An Integer representing the year, where positive values represent years A.D., 0 represents 1 B.C., -1 represents 2 B.C., and so on.

Example

This example uses Year() to return the year from a given Date:

Integer year = Date.Year( Date.Now() )

if ( year % 4 == 0 && year % 100 != 0 )
    Echo( year, " is a leap year." )
else
    Echo( year, " is not a leap year." )
end

The output of this example is:

1998 is not a leap year.

 Copyright © 2021 OpenText Corporation. All rights reserved.