Date and Time Values in SenseTalk
SenseTalk includes commands and functions for working with dates and times. One date or time can be subtracted from another by using the minus ( - ) operator, to get their difference in seconds. A time interval can also be added to a date or time to obtain a new date/time value (see Adding and Subtracting Time Intervals).
Dates, Times, and Time Intervals
SenseTalk makes no fundamental distinction between a "date" and a "time"—both are treated as precise instants in the flow of time, or points along a timeline whose origin (zero value) was at the stroke of midnight at the beginning of January 1, 2001, Coordinated Universal Time. Any date or time before that instant is treated internally as a negative value, and later times as a positive value indicating the number of seconds since the origin.
SenseTalk can recognize dates and times expressed in a wide variety of formats such as "4/22/67" or "1967-04-22 18:00." See the timeInputFormat
global property for details. The Natural
format allows even more variations, such as "May 15, 2004 10:57 PM", or even "yesterday", "today", or "next Tuesday in the afternoon." The words today
and now
(without enclosing quotes) can be used to indicate the current date or the current date and time (unless they are used as variables and assigned some other value). See Natural
Format for more information about this format.
Whenever a date value is supplied without a time of day, it is taken to mean noon of that day. When a time is given without a date, it is assumed to mean the indicated time on the current date (today). All dates and times are assumed to be in the local time zone, as currently set on the machine where the script is running.
A time interval or duration is a length of time. When dealing with time intervals, SenseTalk assumes durations measured in seconds if no specific unit is given, but also allows many other units to be used including weeks, days, hours, milliseconds, etc. (see Time Intervals in Values).
In addition to durations that can be precisely measured in seconds, SenseTalk also supports calendar durations measured in months, including months, calendarQuarters, calendarYears, etc. To see the full list of calendarDuration units that are available, use this command:
put (each item of the unitNames whose unitType is calendarDuration) sorted
See Calendar Durations for more information.
Both time intervals and calendar durations can be used with the ago
and hence
operators to produce a time value that is a length of time in the past or future.
Because there is no real difference between dates and times, this document sometimes refers to either a date or a time as "a date/time value."
Related Global and Local Properties
SenseTalk includes several global or local properties that you can use to affect the behavior of date and time values in your scripts. You will see these properties mentioned in the descriptions of concepts below. Full definitions of each property can be found on Local and Global Properties for Working with Values. The specific properties are:
The ClockFormat
The TimeFormat
The TimeInputFormat
The CenturyCutoff
The LastDayOfMonthCalculation
Date/Time Arithmetic
Adding and Subtracting Time Intervals
Starting from any date (or time), you can obtain a different date/time by simply adding or subtracting a time interval.
Example:
put today + 2 weeks into dueDate
Example:
subtract 3 hours 14 minutes 22 seconds from timer
Calculating Date or Time Differences
By subtracting one date or time value from another, you can easily calculate the number of days between dates or the elapsed time for some process. The difference is always a time interval expressed in seconds, but you can convert it to a different unit (such as days) by dividing it by the number of seconds in that unit (which can be expressed using a time interval expression such as 1 day
, for example). Or you can use the as
operator to convert it to a different duration unit.
Example:
put (expirationDate - "today") / 1 day into daysRemaining -- results in a pure number
put (expirationDate - "today") as days into daysRemaining -- results in a number with units of days
Example:
put the time into startTime -- Start timing here
run somethingTimeConsuming -- Whatever you want to time
put the time - startTime into secondsElapsed
Date or Time Comparisons
The SenseTalk comparison operators (is
, =
, comes before
, <
, and the like) ordinarily treat the two values being compared as text, unless they are both numbers or it "knows" they are both date or time values. Because comparisons usually treat values as text, the following will not produce the desired result:
if the date is between "Sep 21" and "Dec 21" then put "Happy Autumn"
To persuade SenseTalk to perform date or time comparisons, use the date()
or time()
functions or the as date
or as time
operators to convert the text to an internal date/time representation. This will work (note that when the year isn’t specified, the current year is assumed, so this example will work in any year):
if the date is between date ("Sep 21") and "Dec 21" as a date then
put "Happy Autumn!"
end if
Commands and Functions for Working with Dates and Times
Below are the specific commands and functions you can use to manipulate date and time values in SenseTalk.
Date
, AsDate
Functions
Behavior: Returns the current date, or the date value for a given expression. The long date
function returns a verbose version of the date, including the current day of the week and the full name of the month. Abbreviated date
and short date
variants provide the date in other formats. The value returned by the date
function, when converted to text, will automatically display a formatted date, as shown in the Examples. Its value may also be treated as a number, representing the exact date and time when the function was called, for use in date/time calculations. When dateExpr is given, returns a value representing noon on the given day (if dateExpr includes a time of day, it is ignored).
The asDate
function also converts the value given in dateExpr to a date, but instead of assigning it the standard date format, the asDate
function will derive the format from the way dateExpr itself is formatted. The asDate function can also be called using the as {a} date
operator.
Syntax:
the { [ long | short | abbr{ev{iated}} ] } date {of dateExpr}
date( {dateExpr} )
asDate( dateExpr )
Example:
put the date --> "10/07/95"
put the short date --> "10/7/95"
put the abbrev date --> "Sat, Oct 7, 1995"
put the long date --> "Saturday, October 7, 1995"
Example:
put date("May 14, 1942") --> "05/14/42"
Example:
put asDate("May 14, 1942") --> "May 14, 1942"
Related:
TimeZoneOffset
, UTCOffset
, SecondsFromGMT
Functions
Behavior: The TimeZoneOffset
function (or its synonym UTCOffset
) returns the difference between the current local time and Coordinated Universal Time or UTC, expressed in units of hours. The SecondsFromGMT
function (referring to the historical term Greenwich Mean Time or GMT) returns the same value expressed as the plain number of seconds (without units).
These functions can be called with one or two optional parameters. If called with one parameter which is a date, they return the local difference from UTC on that given date (which may vary depending on whether or not daylight savings is in effect on that date). If called with one parameter which is a time zone, they return the difference between that time zone and UTC on the current date. If called with both a date and a time zone (in either order), these functions return the difference between the specified time zone and UTC, on the given date.
When specifying a time zone you may use its official id name (e.g. "America/Los_Angeles"), the time zone abbreviation ("PDT"), or any unique part of the name, ignoring spaces and special characters (e.g. "los angeles").
Syntax:
the [ TimeZoneOffset | UTCOffset | SecondsFromGMT ] {of aDateOrTimeZone } [ TimeZoneOffset | UTCOffset | SecondsFromGMT ]( {aDateOrTimeZone} ) [ TimeZoneOffset | UTCOffset | SecondsFromGMT ]( aDate , aTimeZone ) [ TimeZoneOffset | UTCOffset | SecondsFromGMT ]( aTimeZone , aDate )
Example:
put the TimeZoneOffset --> -7 hours (example shown in Mountain Standard Time)
put the SecondsFromGMT --> -25200 (the same value as a plain number of seconds)
put the TimeZoneOffset of "July 1" --> -6 hours (July is in Mountain Daylight Time)
Example:
put TimeZoneOffset("March 4, 2001", "Denver") --> -7 hours
put UTCOffset("Paris", "2023-07-10") --> 2 hours
Related:
Seconds
Function
Behavior: Returns the current number of seconds since the beginning of January 1, 2001. The long seconds
function returns a more precise version of the seconds, including the current fraction of a second. Abbreviated seconds
and short seconds
variants are also available, which provide values rounded to the microsecond (6 decimal places) and millisecond (3 decimal places), respectively. If a dateTimeValue is given, the number returned will be the number of seconds since the beginning of January 1, 2001, until the given time (a negative number if the given time is earlier than 2001).
Syntax:
the { [ long | short | abbr{ev{iated}} ] } seconds
the seconds {of dateTimeValue}
seconds( {dateTimeValue} )
Example:
put the seconds --> 62899676
Example:
put the long seconds --> 62899676.90865231
put abbreviated seconds --> 62899676.908652
put the short seconds --> 62899676.908
Related:
Ticks
Function
Behavior: Returns the number of ticks (1/60 second) since the SenseTalk engine was started.
Syntax:
the ticks
ticks()
Example:
if the ticks is greater than 36000 then
put "SenseTalk was started more than 10 minutes ago."
end if
Related:
Time
, AsTime
Functions
Behavior: Returns the current time of day, or the time value of a given expression. The long time
function returns a longer version of the time, including the seconds. Abbreviated time
and short time
variants provide the time in other formats.
The asTime function also converts the value given in timeExpr to a time, but instead of assigning it the standard time format, the asTime function will derive the format from the way timeExpr itself is formatted. The asTime function can also be called using the as {a} time
operator.
Syntax:
the { [ long | short | abbr{ev{iated}} ] } time {of timeExpr}
time( {timeExpr} )
asTime( {timeExpr} )
Example:
put the time --> 02:38 PM
put the short time --> 02:38
put the abbrev time --> 02:38:32
put the long time --> 02:38:32 PM
Example:
put time("7:35:42")--> 07:35 AM
Example:
put asTime("7:35:42") --> 07:35:42
The value returned by the time
function, when converted to text, will automatically display a formatted time, as shown in the Examples. Its value may also be treated as a number, representing the exact date and time when the function was called (or the exact date and time represented by its parameter), for use in date/time calculations or comparisons.
If the clockFormat
global property is set to "24 hour", the format used by all of the time functions will not include "AM" or "PM" but instead will indicate hours between 00 and 23.
When timeExpr is given, that value is evaluated and returned as an internal time representation.
The asTime
function also converts the value given in timeExpr to a time, but instead of assigning it the standard time format, the asTime
function will derive the format from the way timeExpr itself is formatted. The asTime function can also be called using the as {a} time
operator.
Related:
Date or Time Components
The following functions provide individual components of a given date or time. Or, when called without a parameter, each function returns a specific component of the current date or time. For example:
the hour
returns the current hourthe hour of "8.45 AM"
returns the hour component of the given time. In this case,8
.
Year
Function
Behavior: Returns the year number of a given date, or the current year.
Syntax:
the year {of dateTimeValue}
year( {dateTimeValue} )
Example:
put the year into currentYear
Example:
put year("4 July 1776") --> 1776
Related:
- The
Date
Function - The
Month
Function - The
MonthName
Function - The
Day
Function - The
WeekdayName
Function - The
DayOfYear
Function
Month
Function
Behavior: Returns the month number (from 1 to 12) of a given date, or the current month.
Syntax:
the month {of dateTimeValue}
month( {dateTimeValue} )
Example:
put the month into monthNum
Example:
put month("4 July 1776") --> 7
Related:
- The
Date
Function - The
Year
Function - The
MonthName
Function - The
Day
Function - The
WeekdayName
Function
MonthName
Function
Behavior: Returns the month name of a given date, or the current month.
Syntax:
the monthName {of dateTimeValue}
monthName( {dateTimeValue} )
Example:
put monthName() into monthName
Example:
put the monthName of "27 April 2022" --> April