Working with date and time

Timestamp

In the previous article, we said that BotBrains stores all user data as numbers. This also applies to date and time data. BotBrains stores the date and time as a timestamp.

Timestamp is the number of seconds since 00:00:00 UTC on January 1, 1970.

Date and time format

Some blocks have a date and time parameter. For example, in the "draw a vertical line" block, you need to specify the date and time of the candlestick on which the vertical line will be drawn.

Date and time must be specified in the following format: YYYY-MM-DD [HH-MM-SS]

Here are some examples of date and time values in proper formats:

  • 2021-09-21 13:45:15

  • 2021-05-27 14:00:00

  • 2021-01-01

  • 2021-08-08 15:12

  • 2021-05-01 12

Date and time in other blocks

If in some block you enter the date and time in the proper format, your robot will detect the date and time value and accordingly convert the specified date and time into a timestamp.

For example, look at the scheme below:

In the "enter value" block, the date and time are specified in the correct format. If we run this robot, we can see that the date and time has been successfully timestamped:

In the same way, you can store the date and time in variables - just use the "set variable complex value" block along with the "value input" block:

As a result of executing this scheme, a timestamp will be assigned to the variable - that is, the variable will contain the number of seconds elapsed since 00:00:00 UTC on January 1, 1970.

If we want to display the value of this variable and see not a timestamp, but a formatted date and time, we need to use the "Date and time format string" format type block:

Timestamps comparison

Timestamps can be compared. A timestamp of a later date is greater than a timestamp of an earlier date.

The diagram below will check if the current timestamp is greater than the date timestamp 2021-05-01:

Using the "time information" block, you can get the following time information:

  1. Timestamp

  2. Day of week

  3. Day number of the year

If we run this robot, then, as expected, the message "True" will be displayed, since the current timestamp will always be greater than any timestamp in the past.

Note that with timestamps, as with any other numbers, you can perform mathematical operations. For example, you can subtract 86400 from the current timestamp to get a timestamp that is 86400 seconds (24 hours) less than the current one.

Last updated