Description
Dates are a very common type of data and giving them the proper format is extremely important in order to present the information adequately. For the same reason, the format of hours needs attention.
In this article, you will learn how to format and calculate dates using Studio Pro activities.
Prerequisites
First of all, we need to know what data format standard we are dealing with. Notice that dates have whether ISO 8601 or RFC 2822 format.
In the case of using ISO 8601, the information follows the format YYYY-MM-DD, for example, 2017-06-01. If we include time in the ISO format, the date and time after the T designator is appended to the end — for example, 2016-06-01T14:41:36-08:00. ISO 8601 also supports multiple date formats, such as YYYY-MM-DD that is 2021-06-10
, which also stands for June 10, 2021.
Now, for RFC 2822, there are many entry options, such as 10 Jun 2021
. This standard includes the abbreviated day of the week, numeric date, a three-letter abbreviation of the month, year, time, and time zone — for example, 01 Jun 2016 14:31:46 -0700.
To successfully use the below instructions, bear in mind that you need to work with one of these formats.
Instructions
Here’s how to format dates in a few easy steps.
Changing the date format
1. Set the variable with the date format allowed
2. Use Export to Format activity
3. Set the "Calculate a value" option and enter the variable name set in step 1 or use Set a value picker
4. Click on the "Transform to" parameter and select the format needed from the drop-down list
5. Save the result using the Assign value to variable activity (it is added by default)
Calculating a date
We can also add or subtract days from a date. Here is how to do it:
1. Add the Calculate date activity to your workflow
2. Set the "Calculate a value" parameter and enter the variable name set in step 1.1 — or use the "Set a value" picker
3. Set the "Action" parameter accordingly (add or subtract)
4. Set the "Time unit" value
5. Input the "Value" parameter according to what you have set in set 3 above
6. Save the result using the Assign value to variable activity (added by default).
Calculating a difference
Finally, we can also calculate a difference between dates. To do that, follow these steps:
1. Set two variables with a valid date format
2. Add the Calculate difference activity
3. Set the "Initial date" parameter using the variable name set in step 1 or use the "Set a value" picker
4. Set the date to subtract the parameter using the variable name set in step 1 or use the "Set a value" picker
5. Save the result using the Assign value to variable activity (added by default)
Example:
1moment().format('MMMM Do YYYY, h:mm:ss a'); // December 19th 2022, 7:47:27 pm
2moment().format('dddd'); // Monday
3moment().format("MMM Do YY"); // Dec 19th 22
4moment().format('YYYY [escaped] YYYY'); // 2022 escaped 2022
5moment().format();
Bear in mind that the MomentJs library methods must be used in the "Calculate a value" parameter of the Assign value to variable activity.