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:
Date Date( Date theDate )
Returns a copy of the specified Date, clearing the time in the copy to 00:00:00. See Time().
theDate | - | Specifies a Date for which a time-cleared copy will be returned. |
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
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().
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. |
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
List DateToList( Date theDate )
Converts a Date to a List with six individual date elements. See ListToDate().
theDate | - | Specifies a Date to convert to a List. |
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) |
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.
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().
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:
|
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.
Integer Day( Date theDate )
Returns the day of the month for a specified Date.
theDate | - | Specifies a Date for which the day of the month is returned. |
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!
Integer DayofWeek( Date theDate )
Returns the day of the week for a specified Date.
theDate | - | Specifies a Date for which the day of the week is returned. |
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.
Integer DayofYear( Date theDate )
Returns the day of the year for a specified Date.
theDate | - | Specifies a Date for which the day of the year is returned. |
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!
Integer Hour( Date theDate )
Returns the number of hours past midnight for a specified Date.
theDate | - | Specifies a Date for which the number of hours past midnight is returned. |
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.
Date ListToDate( List theList )
Converts a List of numerical date elements to a new Date.
theList | - |
Specifies a List to convert to a Date. It must contain the following Integer elements:
|
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.
Integer Minute( Date theDate )
Returns the number of minutes past the hour from a given Date.
theDate | - | Specifies a Date for which the minute past the hour is returned. |
Integer Month( Date theDate )
Returns the index of the month for a specified Date.
theDate | - | Specifies a Date for which the month is returned. |
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.
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().
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: 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. |
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().
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). |
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
Date Now()
Returns the current date and time.
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
Integer Second( Date theDate )
Returns the number of seconds past the minute for a given Date.
theDate | - | Specifies a Date for which the number of seconds past the minute is returned.. |
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().
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:
|
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" )
Integer Systime()
Returns the current value of the server system clock, timed in seconds.
This function is most often used to compare two system clock values to calculate timing in seconds. See 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().
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 841ms to count to a hundred thousand in Oscript.
Date Time( Date theDate )
Returns a copy of the specified Date, clearing the date portion of the copy. See Date().
theDate | - | Specifies a Date for which a date-cleared copy will be returned. |
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.
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().
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. |
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.
Integer Year( Date theDate )
Returns the year for a specified Date.
theDate | - | Specifies a Date for which the year is returned. |
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.