TiKiLa Date and Time Functions
An overview of the TiKiLa functions to handle, parse and manipulate date and time objects and strings.
TiKiLa provides a set of functions to work with date and time objects. Here is an overview with examples:
Many of the functions make use of strftime (”string format time”). This is a common shorthand way to define the format of date and time strings. See the cheat sheet below for some common formats and check out this interactive playground.
strftime Cheat Sheet
Code | Example | Description |
%a | Sun | Weekday as locale’s abbreviated name. |
%A | Sunday | Weekday as locale’s full name. |
%w | 0 | Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. |
%d | 08 | Day of the month as a zero-padded decimal number. |
%-d | 8 | Day of the month as a decimal number. (Platform specific) |
%b | Sep | Month as locale’s abbreviated name. |
%B | September | Month as locale’s full name. |
%m | 09 | Month as a zero-padded decimal number. |
%-m | 9 | Month as a decimal number. (Platform specific) |
%y | 13 | Year without century as a zero-padded decimal number. |
%Y | 2013 | Year with century as a decimal number. |
%H | 07 | Hour (24-hour clock) as a zero-padded decimal number. |
%-H | 7 | Hour (24-hour clock) as a decimal number. (Platform specific) |
%I | 07 | Hour (12-hour clock) as a zero-padded decimal number. |
%-I | 7 | Hour (12-hour clock) as a decimal number. (Platform specific) |
%p | AM | Locale’s equivalent of either AM or PM. |
%M | 06 | Minute as a zero-padded decimal number. |
%-M | 6 | Minute as a decimal number. (Platform specific) |
%S | 05 | Second as a zero-padded decimal number. |
%s | 1689162180 | Unix Epoch - seconds since 01/01/1970 |
%-S | 5 | Second as a decimal number. (Platform specific) |
%f | 000000 | Microsecond as a decimal number, zero-padded to 6 digits. |
%z | +0000 | UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive). |
%Z | UTC | Time zone name (empty string if the object is naive). |
%j | 251 | Day of the year as a zero-padded decimal number. |
%-j | 251 | Day of the year as a decimal number. (Platform specific) |
%U | 36 | Week number of the year (Sunday as the first day of the week) as a zero-padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. |
%-U | 36 | Week number of the year (Sunday as the first day of the week) as a decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. (Platform specific) |
%W | 35 | Week number of the year (Monday as the first day of the week) as a zero-padded decimal number. All days in a new year preceding the first Monday are considered to be in week 0. |
%-W | 35 | Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. (Platform specific) |
%c | Sun Sep 8 07:06:05 2013 | Locale’s appropriate date and time representation. |
%x | 09/08/13 | Locale’s appropriate date representation. |
%X | 07:06:05 | Locale’s appropriate time representation. |
%% | % | A literal '%' character. |
TiKiLa Date and Time Functions
formatTime(datetime_object, format_in_strftime)
This function takes a datetime object and a format in strftime form, and outputs a formatted string.
Example:
formatTime("2022-02-02T11:57:44+00:00", "%Y-%m-%d") —> 2022-02-02
formatTime("2023-07-01T09:52:56+01:00", "%Y-%m-%d %H:%M") —> 2023-07-01 09:52
parseTime(timestring, format_in_strftime)
This function takes the a datetime string and its format in strftime and converts it into a valid datetime object.
Example:
parseTime("10/05/2023 09:00:00", "%d/%m/%Y %H:%M:%S") —> 2023-05-10T09:00:00+00:00
parseTime("2023-05-10 09:00+00:00", "%Y-%m-%d %H:%M%z") —> 2023-05-10T09:00:00+00:00
timeAt(datetime_object, timezone_string)
This function takes a datetime object and a timezone string and returns a datetime object that represents the time in the given timezone.
Example:
timeAt("2023-06-21T11:57:44+03:00", "UTC") —> 2023-06-21T08:57:44+00:00
timeAt("2023-06-21T01:32:00+03:00", "America/New_York") —> 2023-06-20T18:32:00-04:00
timeDuration(start_datetime_object, end_datetime_object)
This function calculates the duration between two datetime objects and returns it in seconds.
Example:
timeDuration("2023-06-21T12:57:00+00:00", "2023-06-21T13:57:00+00:00") —> 3600
timeDuration("2023-06-21T12:57:00+00:00", "2023-07-05T00:57:00+01:00") —> 1162800
timeNow(timezone_string)
This function returns the current date and time in the specified timezone.
Example:
timeNow("America/Los_Angeles") —> 2023-06-21T06:15:49-07:00
timeNow("UTC") —> 2023-06-21T13:16:20+00:00
unixToTime(unix_epoch, timezone_string)
This function takes a Unix epoch time and a timezone string and returns a datetime object representing the time in the given timezone.
Example:
unixToTime(1653214800, "America/New_York") —> 2022-05-22T06:20:00-04:00
unixToTime(1665360000, "UTC") —> 2022-10-10T00:00:00+00:00
Video Examples
timeNow & timeAt
formatTime & parseTime
timeDuration
unixToTime
Last updated on June 21, 2023