DateTime API

🚧

Javascript

Javascript has similar, but different API.

When using date and time variables, often we need to format them in different ways or extract just a certain value out of an entire timeframe.

In order to get the exact value we need, we can add some methods to our expression to do some basic calculations.

Here are some examples:

🚧

Example

Examples might be different, because those presented below are based on current time

List of DateTime methods

DateTime methodDescription
toDateTime(format)Converts a string into a DateTime value, formatted description.
getYear()Returns the year value of the current time
getMonth()Returns the month value of the current time
getDay()Returns the day value of the current time
getHour()Returns the hour value of the current time
getMinute()Returns the minute value of the current time
getDayOfWeek()Returns the day of the week value of the current time
formatAsTime()Returns time string in format HH:mm of the current time
formatAsDate()Returns date string in format yyyy-MM-dd of the current time
format(pattern: string)Returns a string in a pattern-based format.
plusYears(number)Adds a number of years to the current time
plusMonths(number)Adds a number of months to the current time
plusDays(number)Adds a number of days to the current time
plusMinutes(number)Adds a number of minutes to the current time
JavaScript methodsLinks to other javascript methods for date and time

toDateTime(format)

Converts a string into a DateTime value, formatted description. See format.

"2010-06-30T01:20+02:00".toDateTime()
"2010-06-30 01:20"

getYear()

returns year

system.currentTime.getYear()
2021

you can add or subtract to it as needed.

If you are in 2022 and add {system.currentTime.getYear()+2} to a response, the result will be: 2024.

1618

getMonth()

returns month of year

system.currentTime.getMonth()
7
1480

getDay()

returns day of month

system.currentTime.getDay()
9

getHour()

returns hour of day

system.currentTime.getHour()
8

You can add or subtract if you need to adjust for the timezone, e.g. system.currentTime.getHour()+2.

getMinute()

returns minute of hour

system.currentTime.getMinute()
52

getDayOfWeek()

returns day of week (Monday 1, Sunday 7)

system.currentTime.getDayOfWeek()
5

Example:

In the Custom block you can first retrieve the current day of the week and assign it to a keyDay key:

1262

In the following block, you can then add this response

996

which includes this snippet of code that checks which day of the week it is and depending on the day will suggest 3 of the following days.

{if (memory.keyDay =1) "Tuesday, Wednesday or Thursday" else if (memory.keyDay =2) "Wednesday, Thursday or Friday" else if (memory.keyDay =3) "Thursday, Friday or Saturday" else if (memory.keyDay =4) "Friday, Saturday or Sunday" else if (memory.keyDay =5) "Saturday, Sunday or Monday" else if (memory.keyDay =6) "Sunday, Monday or Tuesday" else if (memory.keyDay =7) "Monday, Tuesday or Wednesday" else ""}?

The response will look like this:

764

formatAsTime()

returns string in format HH:mm

system.currentTime.formatAsTime()
08:54
1286

formatAsDate()

returns string in format yyyy-MM-dd

system.currentTime.formatAsDate()
2021-07-09
1366

format(pattern: string)

format(pattern: string) returns a string in a pattern-based format. You can find guidelines on the pattern to follow here.

system.currentTime.format("d MMMM, yyyy")
9 July, 2021

Example:

In the expression below, the time value is not formatted.

This is what it looks like in the bot response.

Now, let's format the time value by adding .format("d MMMM, yyyy") to the expression

See now how the formatting looks different in the response:

plusYears(number)

Another way to add or subtract a number (years, months, days, etc.) from the current time is by adding .plus at the end of the expression.

system.currentTime.plusYears(1)
// current date is 2021-07-09
2022-07-09 08:56
1642

plusMonths(number)

system.currentTime.plusMonths(1)
// current date is 2021-07-09
2021-08-09 08:56
1538

plusDays(number)

system.currentTime.plusDays(1)
// current date is 2021-07-09
2021-07-10 08:58
1622

plusMinutes(number)

system.currentTime.plusMinutes(1)
// current date is 2021-07-09 08:58

2021-07-09 08:59
1616

What’s Next