Datetime Functions¶
In Mythradon, date and date-time values are formatted as strings in the YYYY-MM-DD HH:mm format, for example, '2021-01-01' or '2021-01-01 10:00'.
Datetime data types are stored in the database in either Coordinated Universal Time (UTC) or Greenwich Mean Time (GMT). When these datetime values are accessed through the API or displayed in the user interface, they are converted to match the timezone offset of the current user. However, when datetime values are used in formula functions, they are presented in UTC/GMT time.
This means that all datetime functions are performed in UTC/GMT time. When such a function is applied to a database record and then retrieved via the user interface or an API, the displayed value will be adjusted according to the user's timezone settings.
Mythradon supports the following datetime functions:
- datetime\addDays()
- datetime\addHours()
- datetime\addMinutes()
- datetime\addMonths()
- datetime\addWeeks()
- datetime\addYears()
- datetime\closest()
- datetime\date()
- datetime\dayOfWeek()
- datetime\diff()
- datetime\format()
- datetime\hour()
- datetime\minute()
- datetime\month()
- datetime\now()
- datetime\today()
- datetime\year()
datetime\addDays( )¶
The datetime\addDays() function is used to add DAYS to a date or datetime VALUE. This will return a new date or datetime. DAYS can be negative if you need to subtract date from a date or datetime VALUE.
Example:
output\printLine( datetime\addDays( '2024-04-14 03:45', 10 ) ); // Prints 2024-04-24 03:45:00
datetime\addHours( )¶
The datetime\addHours() function increases or decreases the datetime value by a specified number of hours. The hours parameter can be either positive or negative. It returns a modified string representing the adjusted datetime value.
Example:
output\printLine( datetime\addHours( '2024-04-14 03:45', 10 ) ); // Prints 2024-04-14 13:45:00
datetime\addMinutes( )¶
The datetime\addMinutes() function increments or decrements the datetime value by a specified number of minutes. The minutes parameter can be either positive or negative. It returns a modified string representing the adjusted datetime value.
Example:
output\printLine( datetime\addMinutes( '2024-04-14 03:45', 10 ) ); // Prints 2024-04-14 03:55:00
datetime\addMonths( )¶
The datetime\addMonths() function adds or subtracts months to a date or datetime value. The months parameter can be positive or negative. It returns a modified string representing the adjusted date or datetime value.
Example:
output\printLine( datetime\addMonths( '2024-04-14 03:45', 10 ) ); // Prints 2025-02-14 03:45:00
datetime\addWeeks( )¶
The datetime\addWeeks() function adds WEEKS to date or datetime VALUE. WEEKS can be negative. Returns a modified STRING value.
Example:
output\printLine( datetime\addWeeks( '2024-04-14 03:45', 10 ) ); // Prints 2024-06-23 03:45:00
datetime\addYears( )¶
The datetime\addYears() function adds or subtracts years to a date or datetime value. The years parameter can be positive or negative. It returns a modified string representing the adjusted date or datetime value.
Example:
output\printLine( datetime\addYears( '2024-04-14 03:45', 10 ) ); // Prints 2034-04-14 03:45:00
datetime\closest( )¶
--
The datetime\closest() function returns the closest date or datetime to the specified VALUE, based on the provided parameters.
TYPE: Defines the unit of comparison. Accepted values include:
- 'time'
- 'minute'
- 'hour'
- 'date'
- 'month'
- 'dayOfWeek'
TARGET: The specific value to match against, provided as an integer or string (depending on TYPE).
IS_PAST (boolean): If true, the function searches for the closest matching value in the past. If false, it searches into the future.
TIMEZONE (optional): The timezone to apply when calculating the result. If omitted, the system's default timezone is used.
This function is useful for scheduling, recurrence calculations, and aligning timestamps to the nearest logical boundary.
Example:
datetime\closest(datetime\now(), 'time', '20:00'); // Returns the closest datetime value in the future with 20:00 time.
datetime\closest('2022-11-20', 'date', 1, true); // Returns `2022-11-01`, the first day of the month.
datetime\closest('2022-11-20', 'date', 1, false); // Returns `2022-12-01`, the first day of the next month.
datetime\closest(datetime\now(), 'dayOfWeek', 1, , 'UTC'); // Returns the next Monday (the beginning of the day UTC time).
datetime\date( )¶
The datetime\date() function retrieves the day of the month (ranging from 1 to 31) from a given date or datetime value. It returns 0 if the VALUE is empty. If no TIMEZONE is specified, the system's default timezone is applied.
Example:
datetime\dayOfWeek( )¶
The datetime\dayOfWeek() function retrieves the day of the week (ranging from 0 to 6), with -1 returned if the VALUE is empty. Sunday is represented by 0. If no TIMEZONE is specified, the system's default timezone is utilised.
Examples:
output\printLine( datetime\dayOfWeek( '2024-04-07', 'UTC' ) ); // Sunday - prints 0
output\printLine( datetime\dayOfWeek( '2024-04-04', 'UTC' ) ); // Thursday - prints 4
datetime\diff( )¶
The datetime\diff() function calculates the difference between two dates or datetimes based on the specified INTERVAL_TYPE, which can be 'years', 'months', 'days', 'hours', or 'minutes'. It returns null in case of an error. The result is negative if VALUE_1 is less than VALUE_2.
Examples:
output\printLine( datetime\diff('2024-04-17', '2024-04-01', 'days') ); // Prints 16
output\printLine( datetime\diff('2024-04-16', '1970-10-07', 'days') ); // Prints 19550
output\printLine( datetime\diff('2024-04-18 01:00', '2024-04-17 00:00', 'hours') ); // Prints 25
datetime\format( )¶
The datetime\format() function transforms a date or datetime VALUE into a string formatted according to the specified timezone and format, or based on the application's default settings. Both TIMEZONE and FORMAT parameters are optional. If TIMEZONE is omitted, the default timezone is applied. If FORMAT is omitted, the default format is used.
Examples:
Date and time fields can be formatted in a variety of ways using the following formatting options:
YYYY- 4 digit year:2023YY- 2 digit year:23M- Single digit month:1..12MM- 2 digit month number:01..12MMM- Short month name:Jan..DecMMMM- Full month name:January..DecemberD- Single digit day of month:1..31DD- 2 digit day of month:01..31Do- Day of month with ordinal:1st..31stddd- Short day of week name:Tuedddd- Full day of week name:TuesdayH- Single digit hour (24 hour time):0..23HH- 2 digit hour (24 hour time):00..23h- Single digit hour (12 hour time):0..12hh- 2 digit hour (12 hour time):00..12aA- post or ante meridiem:amPMm- Single digit minutes:0..59mm- 2 digit minutes:00..59s- Single digit seconds:0..59ss- 2 digit seconds:00..59Z ZZ zz- offset from UTC :+02:00 +0200zz- timezone:Europe/London
datetime\hour( )¶
The datetime\hour() function retrieves the hour from a datetime VALUE, expressed in a 24-hour format (ranging from 0 to 23). It returns -1 if the VALUE is empty. If the TIMEZONE parameter is omitted, the function defaults to using the system's timezone.
Example:
datetime\minute( )¶
The datetime\minute() function extracts the minutes (0-59) from a datetime VALUE. It returns -1 if the VALUE is empty. If no TIMEZONE is specified, the function defaults to the system's timezone.
Example:
output\printLine( datetime\minute('2024-04-16 15:45', 'UTC') ); // Prints 45
datetime\month( )¶
The datetime\month() function returns the month (ranging from 1 to 12) from a datetime VALUE. If the VALUE is empty, it returns 0. If the TIMEZONE parameter is omitted, the system's default timezone is applied.
Example:
datetime\now( )¶
The datetime\now() function retrieves the current system datetime in the format 'YYYY-MM-DD HHss'.
Note
The datetime\now() function will always return the date/time in GMT/UTF time regardless of your timezone setting.
Example:
output\printLine( datetime\now() ); // Prints the current datetime. I.e. 2024-04-16 02:07:56
datetime\today( )¶
The datetime\today() function returns the current date in the 'YYYY-MM-DD' format, excluding the time component.
Example:
datetime\year( )¶
The datetime\year() function returns the year from a datetime VALUE. If the VALUE is empty, it returns 0. If the TIMEZONE parameter is omitted, the system's default timezone is applied.
Example:
See Also¶
- Array Functions
- Datetime Functions
- Entity Functions
- Env Functions
- Ext Functions
- JSON Functions
- Language Functions
- Number Functions
- Object Functions
- Output Functions
- Password Functions
- Record Functions
- String Functions
- Mythradon Marketing
- Mythradon Sales
- Mythradon Service
- Mythradon System Administration
- Mythradon Tools