Navigation
Synopsis Date and time values.
Syntax
  1. $ Date
  2. $ Time
  3. $ DateTime
Types datetime
Usage import DateTime; (included in Prelude)
Description Date, time, and datetime values are represented by the datetime type. datetime literals start with a $ and are made up of either a date, given in year, month, day of month order; a time, preceded by T and given in hour, minute, second, millisecond, (optional) timezone offset order; or a datetime, which is a date and a time, in the orders given above, and separated by a T.

The following fields provide access to information about the value, but cannot be set:
  • isDate: returns true if the value is a date value, false if the value is a datetime or time value.
  • isTime: returns true if the value is a time value, false if the value is a date or datetime value.
  • isDateTime: returns true if the value is a datetime value, false if the value is a date or time value.
  • justTime: returns the date component of a date or datetime value.
  • justDate: returns the time component of a time or datetime value.
  • century: returns the century component of a year for date or datetime values.
The following fields provide access to the individual components of date, time and datetime values, and can be accessed using DateTime/FieldSelection and be assigned using DateTime/FieldSelection:
  • year
  • month
  • day
  • hour
  • minute
  • second
  • millisecond
  • timezoneOffsetHours
  • timezoneOffsetMinutes
Not all fields are available on all values as indicated by the following table:
Field date datetime time
year x x
month x x
day x x
hour x x
minute x x
second x x
millisecond x x
timezoneOffsetHours x x
timezoneOffsetMinutes x x
The isDate, isTime, and isDateTime fields can be checked in advance to determine what kind of value is stored in a variable of type datetime.

The following operators are defined for DateTime: The following functions are defined for DateTime:
  • createDate: Create a new date.
  • createDateTime: Create a new datetime (with optional timezone offset).
  • createDuration: Create a new duration representing the duration between the begin and end dates.
  • createInterval: Given two datetime values, create an interval.
  • createTime: Create a new time (with optional timezone offset).
  • dateRangeByDay: Given an interval, return a list of days.
  • daysDiff: Return the difference between two dates and/or datetimes in days.
  • daysInInterval: Return the number of days in an interval, including the begin and end days.
  • decrementDays: Decrement the days by a given amount or by 1.
  • decrementHours: Decrement the hours by a given amount or by 1.
  • decrementMilliseconds: Decrement the milliseconds by a given amount or by 1.
  • decrementMinutes: Decrement the minutes by a given amount or by 1.
  • decrementMonths: Decrement the months by a given amount or by 1.
  • decrementSeconds: Decrement the seconds by a given amount or by 1.
  • decrementYears: Decrement the years by a given amount or by 1.
  • Duration: A duration of time, measured in individual years, months, etc.
  • incrementDays: Increment the days by given amount or by 1.
  • incrementHours: Increment the hours by a given amount or by 1.`
  • incrementMilliseconds: Increment the milliseconds by a given amount or by 1.
  • incrementMinutes: Increment the minutes by a given amount or by 1.
  • incrementMonths: Increment the months by a given amount or by 1.
  • incrementSeconds: Increment the seconds by a given amount or by 1.
  • incrementYears: Increment the years by given amount or by 1.
  • interval: A closed interval on the time axis.
  • joinDateAndTime: Create a new datetime by combining a date and a time.
  • now: Get the current datetime.
  • parseDate: Parse an input date given as a string using the given format string.
  • parseDateInLocale: Parse an input date given as a string using a specific locale and format string.
  • parseDateTime: Parse an input datetime given as a string using the given format string.
  • parseDateTimeInLocale: Parse an input datetime given as a string using a specific locale and format string.
  • parseTime: Parse an input time given as a string using the given format string.
  • parseTimeInLocale: Parse an input time given as a string using a specific locale and format string.
  • printDate: Print an input date using the given format string.
  • printDateInLocale: Print an input date using a specific locale and format string.
  • printDateTime: Print an input datetime using the given format string.
  • printDateTimeInLocale: Print an input datetime using a specific locale and format string.
  • printTime: Print an input time using the given format string.
  • printTimeInLocale: Print an input time using a specific locale and format string.
  • splitDateTime: Split an existing datetime into a tuple with the date and the time.
Examples Examples of datetime values are:
rascal>$2010-07-15$;
datetime: 2010-07-15
rascal>$T07:15:23.123+0100$;
datetime: $T07:15:23.123+01:00$
Now introduce a datetime value and assign it to DT.
rascal>DT = $2010-07-15T09:15:23.123+03:00$;
datetime: $2010-07-15T09:15:23.123+03:00$
Here are examples of some datatime fields:
rascal>DT.isDateTime;
bool: true
rascal>DT.justDate;
datetime: 2010-07-15
rascal>DT.justTime;
datetime: $T09:15:23.123+03:00$
rascal>DT.century;
int: 20
Pitfalls In normal parlance, the year 2010 is in the 21th century. The century field, however, just returns the century component of a given year, e.g., for 2010 this is 20.
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.