Formulas: Dates
Brian Holthouse avatar
Written by Brian Holthouse
Updated over a week ago

Date and datetime formulas allow you to work with date and time datapills. These formulas are whitelisted Ruby methods, and therefore not all Ruby methods are supported.

You can refer to the syntax and sample uses of these commands by clicking the links below:


Date Arithmetic

Use the following keywords to perform arithmetic with date and datetime data:

  • seconds

  • minutes

  • days

  • months

  • years

When combined with a formula, you can perform addition and subtraction.

Sample usage

Date Arithmetic

Output

"2020-01-01".to_date + 2.days

"2020-01-03"

"2020-01-01".to_date - 2.days

"2019-12-30"

"2020-01-01".to_date + 2.months

"2020-03-01"

"2020-01-01".to_date - 2.months

"2019-11-01"

"2020-01-01".to_date + 2.years

"2022-01-01"

"2020-01-01".to_date - 2.years

"2018-01-01"


now

Returns the time and date at runtime in US Pacific Time Zone (PST).

Sample usage

Formula

Result

now

"2022-02-01T07:00:00.000000-08:00"

now + 8.hours

"2022-02-01T15:00:00.000000-08:00"

now + 2.days

"2022-02-03T07:00:00.000000-08:00"

How it works

The formula calculates the timestamp when the a job is being processed. Each step using this formula will return the timestamp at which the step runs.


today

Returns the date at runtime in US Pacific Time Zone.

Sample usage

Formula

Result

today

"2022-02-01"

today + 8.hours

"2022-02-01T15:00:00.000000-08:00"

today + 2.days

"2022-02-03"

How it works

The formula calculates the timestamp when the a job is being processed. Each step using this formula will return the date at which the step runs.


from_now

Returns a future timestamp by a specified time duration. The timestamp is calculated at runtime.

Syntax

Unit.from_now

  • Unit - A time value to offset.

Sample usage

Formula

Result

30.seconds.from_now

"2022-02-01T07:00:30.000000-08:00"

2.months.from_now

"2022-04-01T07:00:00.000000-08:00"

3.days.from_now

"2022-02-04T07:00:00.000000-08:00"

How it works

The formula calculates the current timestamp and offsets by a specified time duration. This timestamp is calculated when the a job is being processed. Each step using this formula will return a timestamp for each step that runs.


ago

Returns an earlier timestamp by a specified time duration. The timestamp is calculated at runtime.

Syntax

Unit.ago

  • Unit - A time value to offset.

Sample usage

Formula

Result

2.months.ago

"2020-10-04 14:45:29 -0700"

3.days.ago

"2020-12-01 14:45:29 -0700"

30.seconds.ago

"2020-12-04 14:15:29 -0700"

How it works

The formula calculates the current timestamp and offsets by a specified time duration. This timestamp is calculated when the a job is being processed. Each step using this formula will return a timestamp for each step that runs.


wday

Returns day of the week. Sunday returns 0, Monday returns 1.

Syntax

Date.wday

  • Date - A date or datetime datatype.

Sample usage

Example

Result

today.wday

4

"01/12/2020".to_date(format:"DD/MM/YYYY").wday

2

How it works

The formula calculates the current day when the a job is being processed. The day of the week is converted into an integer output. Sunday = 0, Monday = 1.


yday

Returns day number of the year.

Syntax

Date.yday

  • Date - A date or datetime datatype.

Sample usage

Example

Result

today.yday

338

"2020-01-01".to_date(format:"YYYY-MM-DD").yday

1

"2020-02-01".to_date(format:"YYYY-MM-DD").yday

32

How it works

The formula calculates the current day when the a job is being processed. The day of the year is converted into an integer output.


yweek

Returns week number of the year.

Syntax

Date.yweek

  • Date - A date or datetime datatype.

Sample usage

Example

Result

today.yweek

49

"2020-01-01".to_date(format:"YYYY-MM-DD").yweek

1

"2020-02-01".to_date(format:"YYYY-MM-DD").yweek

5

How it works

The formula calculates the current day when the a job is being processed. The week of the year is converted into an integer output.


beginning_of_hour

Returns datetime for top-of-the-hour for a given datetime.

Syntax

Datetime.beginning_of_hour

  • Datetime - An input datetime.

Sample usage

Formula

Result

today.to_time.beginning_of_hour

"2020-12-02T16:00:00.000000-07:00"

"2020-06-01T01:30:45.000000+00:00".beginning_of_hour

"2020-06-01T01:00:00.000000+00:00"

"2020-06-01".to_time.beginning_of_hour

"2020-06-01T00:00:00.000000+00:00"


beginning_of_day

Returns datetime for midnight on date of a given date/datetime.

Syntax

Date.beginning_of_day

  • Date - An input date or datetime.

Sample usage

Formula

Result

today.beginning_of_day

"2020-12-02T00:00:00.000000-07:00"

"2020-06-01".to_date.beginning_of_day

"2020-06-01T00:00:00.000000+00:00"

"2020-06-01T01:30:45.000000+00:00".beginning_of_day

"2020-06-01T00:00:00.000000+00:00"


beginning_of_week

Returns date of the previous Monday for a given date/datetime.

Syntax

Date.beginning_of_week

  • Date - An input date or datetime.

Sample usage

Formula

Result

today.beginning_of_week

"2020-11-30T00:00:00.000000+00:00"

"2020-06-01".to_date.beginning_of_week

"2020-06-01T00:00:00.000000+00:00"

"2020-06-01T01:30:45.000000+00:00".beginning_of_week

"2020-06-01T00:00:00.000000+00:00"


beginning_of_month

Returns first day of the month for a given date/datetime.

Syntax

Date.beginning_of_month

  • Date - An input date or datetime.

Sample usage

Formula

Result

today.beginning_of_month

"2020-12-01T00:00:00.000000+00:00"

"2020-06-01".to_date.beginning_of_month

"2020-06-01T00:00:00.000000+00:00"

"2020-06-01T01:30:45.000000+00:00".beginning_of_month

"2020-06-01T00:00:00.000000+00:00"


beginning_of_year

Returns first day of the year for a given date/datetime.

Syntax

Date.beginning_of_year

  • Date - An input date or datetime.

Sample usage

Formula

Result

today.beginning_of_year

"2020-01-01T00:00:00.000000+00:00"

"2020-06-01".to_date.beginning_of_year

"2020-01-01T00:00:00.000000+00:00"

"2020-06-01T01:30:45.000000+00:00".beginning_of_year

"2020-01-01T00:00:00.000000+00:00"


end_of_month

Returns last day of the month for a given date/datetime. This formula will return a date or datetime based on the input data.

Syntax

Date.end_of_month

  • Date - An input date or datetime.

Sample usage

Formula

Result

today.end_of_month

"2020-12-31"

"2020-06-01".to_date.end_of_month

"2020-06-30"

"2020-06-01T01:30:45.000000+00:00".to_time.end_of_month

"2020-06-30T23:59:59.999999+00:00"


strftime

Returns a datetime input as a user-defined string.

Syntax

Date.strftime(format)

  • Date - An input date or datetime.

  • format - The format of the user-defined datetime written as a string.

Sample usage

Formula

Result

"2020-06-05T17:13:27.000000-07:00".strftime("%Y/%m/%d")

"2020/06/05"

"2020-06-05T17:13:27.000000-07:00".strftime("%Y-%m-%dT%H:%M:%S%z")

"2020-06-05T17:13:27-0700"

"2020-06-05T17:13:27.000000-07:00".strftime("%B %e, %l:%M%p")

"June 5, 5:13 pm"

"2020-06-05T17:13:27.000000-07:00".strftime("%A, %d %B %Y %k:%M")

"Friday, 05 June 2020 0:00"


in_time_zone

Converts a date or datetime to a different timezone using timezone names from the IANA time zone database(opens new window). This formula will return a datetime.

Syntax

Date.in_time_zone(format)

  • Date - An input date or datetime.

  • format - (optional) The target timezone.

Sample usage

Formula

Result

today.in_time_zone("America/New_York")

"2020-12-01T20:00:00.000000-04:00"

today.to_time.in_time_zone("America/New_York")

"2020-12-01T20:00:00.000000-04:00"

"2020-06-01".to_time.in_time_zone

"2020-05-31T20:00:00.000000-04:00"

"2020-06-01T01:30:45.000000+00:00".in_time_zone

"2020-05-31T12:30:00.000000-05:00"


dst?

Returns true if the input datatime is within Daylight Savings Time.

Syntax

Datetime.dst?

  • Datetime - An input date or datetime.

Sample usage

Formula

Result

today.dst?

false

today.in_time_zone("America/New_York").dst?

true

"2020-06-01".in_time_zone("America/New_York").dst?

true

"2020-09-06T18:30:15.671720-05:00".dst?

true


to_date

This formula converts the input data into a date. Returns the date formatted as YYYY-MM-DD.

Syntax

String.first(format: format)

  • String - An input datetime or a string that describes a date or datetime.

  • format - (optional) The date format of the input written as a string. If not specified, Hive Automate will parse the input string automatically.

Sample usage

Formula

Result

"23-01-2020 10:30 pm".to_date(format: "DD-MM-YYYY")

"2020-01-23"

"01-23-2020 10:30 pm".to_date(format: "MM-DD-YYYY")

"2020-01-23"

"2020/01/23".to_date(format: "YYYY/MM/DD")

"2020-01-23"


to_time

Converts a string to an ISO timestamp. The response will use the UTC timezone (+00:00).

Syntax

String.to_time(format)

  • String - An input string that describes a date or datetime.

  • format - The format of the user-defined datetime written as a string.

Sample usage

Formula

Result

"2020-04-02T12:30:30.462659-07:00".to_time

"2020-04-02T19:30:30.462659+00:00"

"2020-04-02".to_time

"2020-04-02T00:00:00.000000+00:00"

How it works

Converts the input string into a datetime datatype. The output datetime will be converted to the UTC timezone (+00:00).


to_i

Convert datetime into epoch time. Returns an epoch time in UTC (+00:00).

Syntax

Datetime.to_i

  • Datetime - An input datetime.

Sample usage

Formula

Result

today.to_time.to_i

1645660800

now.to_i

1645714000

How it works

Converts the input datetime into an integer, it will return epoch time in seconds, not milliseconds. The output datetime will be converted to the UTC timezone (+00:00).

Did this answer your question?