If your testing goals include using dates in a specific format, consider using a JavaScript snippet! This guide provides an overview of the GetDateComponents.js snippet, which returns an object containing values associated with today's date. It can be further modified to produce dates in specific formats and to generate other dates, such as yesterday or two weeks from today.
In this guide, you can learn:
How it works
Snippet output
The GetDateComponents snippet returns an object with values for the day, month, year, weekday, and time. The following code block shows an example of the output that is returned from the GetDateComponents snippet:
{ "dayNumeric": "14", "dayTwoDigit": "14", "dayOrdinal": "14th", "monthNumeric": "9", "monthTwoDigit": "09", "monthNarrow": "S", "monthShort": "Sep", "monthLong": "September", "yearNumeric": "2022", "yearTwoDigit": "22", "weekdayNarrow": "W", "weekdayShort": "Wed", "weekdayLong": "Wednesday", "hour12TwoDigit": "10", "hour24TwoDigit": "10", "hour12Numeric": "10", "hour24Numeric": "10", "dayPeriod": "AM", "minute": "23", "second": "49" }
If left unmodified, the GetDateComponents snippet will return values associated with the current date. If you are only interested in returning the current date, you can skip ahead to the section on how to use this snippet in your tests.
If you would like to return a different date or adjust the time zone, continue reading.
Returning other dates
The GetDateComponents snippet includes custom methods that can return the values associated with the following dates:
- Tomorrow:
today.addDays(1)
- Yesterday:
today.addDays(-1)
- Last Sunday:
today.getPreviousWeekday("sunday")
- Next Friday:
today.getPreviousWeekday("friday").addDays(7)
- Last Month:
today.addMonths(-1)
- Next Year:
today.addYears(1)
These custom methods can be modified and chained. For example, this method would return the date values for the day that is two months from next Monday: today.getPreviousWeekday("monday").addDays(7).addMonths(2)
In the following example, an existing snippet is updated to return an object containing values associated with tomorrow's date:
- Add
//
beforedate = today;
to comment it out. - Remove
//
beforedate = today.addDays(1);
- Click Run to check the result. You should see values associated with tomorrow's date.
Returning specific dates
Using the GetDateComponents snippet, you can return values associated with a specific date by making the following two changes:
- Add a parameter, such as
input_date
with the specific date as the value. The parameter value should be a value that can be parsed by the Date() constructor. - Add the parameter
input_date
as a parameter to the date constructor:var today = new Date(input_date)
.
In this example, the snippet returns values associated with September 12, 1951:
Adjusting the time zone
By default, the variable timezone
is an empty string: let timezone = "";
To return date values associated with a specific timezone, update the timezone
variable with the timezone you would like to use.
For example, to return values associated with the date Sydney's timezone: let timezone = "Australia/Sydney";
How to use it
To use the GetDateComponents snippet in the mabl Trainer, you'll need to take two steps:
- Save the output to a variable
- Create a new variable(s) using the date variable
Saving output to a variable
- Click on the {x} variables button at the bottom of the Trainer window.
- Select Create a new variable.
- Set the variable source to JavaScript.
- If this snippet doesn't exist in your workspace, click on the New button, paste the entire GetDateComponents snippet into the code editor, and save the snippet.
- If this snippet does exist in your workspace, select it from the snippet dropdown.
- Give the variable a name.
- Click OK.
For example, the output for the current date can be saved in a variable called "today":
Creating formatted dates
You can use the values in the GetDateComponents variable to create new variables with the date in a specific format.
For example, this variable uses values from the "today" variable to generate the current date in the mm/dd/yyyy format:
- Click on the {x} variables button at the bottom of the Trainer window.
- Select Create a new variable.
- Set String template as the source.
- Write the string template using this syntax:
{{@today.monthTwoDigit}}/{{@today.dayTwoDigit}}/{{@today.yearNumeric}}
.
- Give the variable a name.
- Click OK.
Some other examples using the variable "today":
Syntax | Example |
---|---|
{{@today.weekdayLong}}, {{@today.monthLong}} {{@today.dayNumeric}}, {{@today.yearNumeric}} |
Wednesday, September 7, 2022 |
{{@today.hour12TwoDigit}}:{{@today.minute}} {{@today.dayPeriod}} |
10:23 AM |
{{@today.monthShort}}-{{@today.dayTwoDigit}}-{{@today.yearTwoDigit}} |
Sep-07-22 |
Next steps
After creating date variables in the desired format, you can use them to assert dates and input date values in your application.