What is even time? Dealing with time sucks no matter what. But, this Tuesday I wanted to talk about some helpful ways to deal with time in Losant.
Comparing Time
Sometimes, we may need to compare one time against another. Let’s look at a simple use case. Let’s say we wanted to compare the time a device reported state to the current time.
From within the Workflow Engine, you will always have a property called time
.
It can always be accessed with templating using {{time}}
. Here is what’s cool, you can use that variable in the Conditional Node:
This works because behind the scenes we are casting time as a number (milliseconds). Since it’s cast to a number, you can even do math with time using the Math Node, which is helpful if you want to add or subtract time.
Time Helpers
We also have cool helpers to help you manipulate time:
{{currentDateTime formatStr}}
The currentDateTime
helper returns the current date/time formatted by the provided format string. (there is also formatDate
if you want to provide a date). Here are some format options:
Behind the scenes, this helper uses a library called Moment.js. If you check out their documentation, you can see all of the formatting options. The currentDateTime
helper is particularly useful if you need to pull one single element(the exact day, or month) from the time. Overall, you can see all the helpers we have in the Accessing Payload Data doc.
We will continue to make time easier. Here is a secret: there is a Time Node on the roadmap. This will make it even easier to do things with time in the Workflow Engine.
That’s all for this week. We’ll be back with a tip next week