Working with dates in mabl

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).

995995

Running GetDateComponents in the Snippet Editor

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:

  1. Add // before date = today; to comment it out.
  2. Remove // before date = today.addDays(1);
  3. Click Run to check the result. You should see values associated with tomorrow's date.
992992

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:

19901990

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:

  1. Save the output to a variable
  2. Create a new variable(s) using the date variable

Saving output to a variable

  1. Click on the {x} variables button at the bottom of the Trainer window.
  2. Select Create a new variable.
  3. Set the variable source to JavaScript.
  4. 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.
  5. If this snippet does exist in your workspace, select it from the snippet dropdown.
  6. Give the variable a name.
  7. Click OK.

For example, the output for the current date can be saved in a variable called "today":

336336

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:

  1. Click on the {x} variables button at the bottom of the Trainer window.
  2. Select Create a new variable.
  3. Set String template as the source.
  4. Write the string template using this syntax: {{@today.monthTwoDigit}}/{{@today.dayTwoDigit}}/{{@today.yearNumeric}}.
336336
  1. Give the variable a name.
  2. Click OK.

Some other examples using the variable "today":

Syntax

Example

{{@today.weedkayLong}}, {{@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.


Did this page help you?