The timeUtils is a powerful library available as a global variable in the Payload Parser, designed to simplify the timezone conversion and manipulation. It offers a set of functions to convert, format, and manage time information with ease.
Available Functions
Compares two dates in their respective timezones. Returns `1` if the first date is greater, `-1` if the second is greater, and `0` if they are equal.
Example:
- const dateA = "2023-01-01T12:00:00.000Z";
- const dateB = "2023-01-01T10:00:00.000-03:00";
- const result = timeUtils.compareTimezones(dateA, "UTC", dateB, "America/Sao_Paulo"); // result will be -1
Converts a date string or object from one timezone to another, returning the result as an ISO string.
Example:
- const convertedTime = timeUtils.convertTimezone("2023-01-01T12:00:00.000Z", "UTC", "America/New_York");
Formats a date in a specific timezone according to the specified format string. The formatting options are based on the chrono `strftime` specification. You can find all available format specifiers in the
chrono documentation.
Example:
- const formattedTime = timeUtils.formatInTimezone(new Date().toISOString(), 'Europe/Paris', '%Y-%m-%d %H:%M:%S %z');
Returns the timezone abbreviation for a given date and timezone (e.g., `EST`, `PST`).
Example:
- const abbreviation = timeUtils.getTimezoneAbbreviation(new Date().toISOString(), "America/New_York"); // e.g., EST or EDT
getTimezoneInfo(timezone)
Retrieves detailed information about a specific timezone.
Example:
- const tzInfo = timeUtils.getTimezoneInfo("America/New_York");
Calculates the offset in minutes from UTC for a given date and timezone.
Example:
- const offset = timeUtils.getTimezoneOffset(new Date().toISOString(), 'America/Sao_Paulo');
isValidTimezone(timezone)
Checks if a given timezone string is valid.
Example:
- const isValid = timeUtils.isValidTimezone("America/Los_Angeles"); // true
- const isInvalid = timeUtils.isValidTimezone("Invalid/Timezone"); // false
listTimezones()
Returns an array of all supported timezone names.
Example:
- const timezones = timeUtils.listTimezones();
nowInTimezone(timezone)
Returns the current time in the specified timezone as an ISO string.
Example:
- const nowInTokyo = timeUtils.nowInTimezone('Asia/Tokyo');
Related Articles
Payload Parser Troubleshooting
When using the Payload Parser on TagoIO, you might run into some common problems that stop the parser from decoding your device data correctly. This means the data won't be saved on TagoIO. These problems are usually related to mistakes in how the ...
Payload Parser - Context & global variables
If you are going to create your own parser, you need to understand how context works. When you start writing your own Payload Parser, you can use certain globals variables in your code. Think of these global variables as variables that you can access ...
Payload Parser
The Payload Parser handles the raw payload sent by the devices in order to extract the measured variables. For example, it can be used to transform an HEX payload sent by a device into temperature and battery levels. You can also use it to handle the ...
Publishing, updating and accessing decoders
TagoIO provides a list of pre-integrated IoT devices for easy connection. However, due to the vast array of manufacturers and ongoing sensor development, you might not find a connector for your specific device. In such cases, you can create your own ...
Building your own parser
In this tutorial, you will learn how to convert (parse) a raw payload sent by a device into actual measurable variables. Data flow structure You can create parses for devices that weren't found in our list of Devices, so that you had to use a ...