Working with Date, Time and DateTime in Airscript
Last updated
Was this helpful?
Last updated
Was this helpful?
Working with dates and times is a notoriously difficult problem, the most famous incantation being Y2K. Airkit provides a comprehensive system to work with dates and times for this precise reason. There are three data types to familiarize yourself with, the , the , and the types. The best way to think of these is by analogy. A Datetime is a specific time marked on your calendar. A Date is a particular day's square on your calendar. A Time is a particular configuration of your clock.
If you would like to find the current Datetime you can use the function.
Other ways to create a date/time are to use the functions , , and . Using January 9th, 2007 9:41 AM as an example Datetime**:**
To retrieve the year of our example Date use the function:
The function can also be used to retrieve the year from our example Datetime:
Use the and the functions to convert a Datetime to a Date or Time respectively:
The following functions can be used to retrieve different parts of Datetimes, Dates, and Times
Returns the date portion of a Datetime.
Returns the time portion of a Datetime.
Returns the year of a Datetime or Date
Returns the month of a Datetime or Date
Returns the day of a Datetime or Date
Returns the hour of a Datetime or Time
Returns the minute of a Datetime or Time
Returns the second of a Datetime or Time
or just the time as 9:41 AM:
This function works equally well on Dates:
Datetimes, Dates, and Times can be modified using the following functions
Changes the date portion of a Datetime.
Changes the time portion of a Datetime.
Changes the timezone of a Datetime.
Changes the year of a Datetime or Date
Changes the month of a Datetime or Date
Changes the day of a Datetime or Date
Changes the hour of a Datetime or Time
Changes the minute of a Datetime or Time
Changes the second of a Datetime or Time
Changes the millisecond of a Datetime or Time
When interfacing with external systems, you'll often encounter Datetimes that are represented either as a Number or a String.
If your particular timestamp looks like it is missing three zeros than the previous example, you are likely working with timestamps in seconds, provide second parameter "s"
Note that one of our previous examples reproduced below included a timezone:
This is important because, in addition to a Date and a Time, Datetimes also have an associated timezone.
<UPDATE_SECOND>
The , , and functions allow you to create a String from a Datetime, Date, or Time. Use these functions when you would like to display a Datetime, Date, or Time to a user so that Airkit can format it correctly according to the user's locale and timezone. For example, to display our example Datetime as January 9, 2007 9:41 AM PST:
If you want to change a particular portion of a Datetime, there are several functions that will allow you to do so. For example, if you wanted to change the year of our example Datetime from 2007 to 2010, you would use the function to create a new Datetime by changing only the year:
If you wanted to update the calendar date of the Datetime, you can use the function to create a new Datetime, with a new calendar date, but the same time:
The and functions allow you to create a new date by adding or removing time units. You could add six months, or 169 days to our example date**:**
An Epoch timestamp is a Number representing the number of seconds (or milliseconds) that have elapsed since January 1st, 1970. For example the timestamp in milliseconds for January 9ith, 2007 is 1168364460000. When you encounter a Datetime in this format look to the function which turns an Epoch timestamp measured in milliseconds into a proper Datetime.
To go in the other direction look no further than the function.
The other commonly used timestamp type is a string with the format "YYYY-MM-DDTHH:mm:ssZ". To convert between a timestamp of this format and a Datetime use the and functions.