TimeUtils

Last Updated on : 2024-06-05 03:14:36download

This topic describes the utility methods related to time.

Convert seconds to an array

API operation name

parseSecond

API description

Convert the specified number of seconds to an array in which each element is a 2-digit string.

Request parameter

Name Data type Description Required
second number The specified number of seconds. Yes
num number The length of each string in the returned array. Default value: 2. No

Return parameter

Name Data type Description
time string The returned array in which the length of each string is specified by num.

Sample request

import { Utils } from "tuya-panel-kit"; const { parseSecond } = Utils.TimeUtils; parseSecond(second, num); // Example parseSecond(111); parseSecond(3333333);

Sample response

['00', '01', '51'] ['25', '55', '33']

Convert seconds to 12-hour time

API operation name

parseHour12

API description

Convert the specified number of seconds to the 12-hour time that is indicated by the specified number.

Request parameter

Name Data type Description Required
num number The specified number of seconds. Yes

Return parameter

Name Data type Description
time string The 12-hour time that is indicated by the specified number.

Sample request

import { Utils } from "tuya-panel-kit"; const { parseHour12 } = Utils.TimeUtils; parseHour12(num); // Example: parseHour12(111); parseHour12(3333333);

Sample response

'01:55 AM' '12:01 PM'

Convert a date to seconds

API operation name

stringToSecond

API description

Convert a specified time point to a number of seconds.

Request parameter

Name Data type Description Required
timeStr string The specified date string. Yes

Return parameter

Name Data type Description
number number The number of seconds between the specified time point and 00:00:00.

Sample request

import { Utils } from "tuya-panel-kit"; const { stringToSecond } = Utils.TimeUtils; stringToSecond(timeStr); // Example: stringToSecond("11:30"); stringToSecond("22:11:30");

Sample response

690 79890

Convert a date to seconds

API operation name

dateToTimer

API description

Convert a specified date string to a number of seconds.

Request parameter

Name Data type Description Required
dateStr string The specified date string. Yes

Return parameter

Name Data type Description
number number The 10-digit value that is converted from dateStr. Unit: seconds.

Sample request

import { Utils } from "tuya-panel-kit"; const { dateToTimer } = Utils.TimeUtils; dateToTimer(dateStr); // Example: dateToTimer("20110801");

Sample response

1312128000

Format current time

API operation name

dateFormat

API description

Convert the current time point to a string in the specified format.

Request parameter

Name Data type Description Required
dateFormate string The month (M), day (d), hour (h), minute (m), second (s), and quarter (q) can use 1–2 placeholders. The year (y) can use 1–4 placeholders. Only one placeholder is used for milliseconds (S) to return a 1 to 3-digit number. Yes

Return parameter

Name Data type Description
date string The string in the specified format that is converted from the current time point.

Sample request

import { Utils } from "tuya-panel-kit"; const { dateFormat } = Utils.TimeUtils; dateFormat(format, date); // Example: new Date().Format("yyyy-MM-dd hh:mm:ss.S"); new Date().Format("yyyy-M-d h:m:s.S");

Sample response

2006-07-02 08:09:04.423 2006-7-2 8:9:4.18

Get the current time zone

API operation name

timezone

API description

Get the current time zone.

Request parameter

None.

Return parameter

Name Data type Description
result string The current time zone.

Sample request

import { Utils } from "tuya-panel-kit"; const { timezone } = Utils.TimeUtils; timezone();

Sample response

+08:00