Dealing with Dates and Times

Dealing with Dates and Times

Because of the ever-changing nature dates and time, test cases that depend on them always need to have the flexibility of parameterization. This flexibility starts with the ability to dynamically change the current date and time.

testRigor supports the following pre-defined variables:
  1. todayYear – (2023) enter stored value "todayYear" into "YYYY" example
  2. todayYearShort – (23) enter stored value "todayYearShort" into "Year" example
  3. todayMonthNumber – (9) enter stored value "todayMonthNumber" into "Month" example
  4. todayMonthNumberTwoDigits – (09) enter stored value "todayMonthNumberTwoDigits" into "Month" example
  5. todayMonth – (September) click stored value "todayMonth" example
  6. todayMonthShort – (Sep) click stored value "todayMonthShort" example
  7. todayDayOfMonth – (2) enter stored value "todayDayOfMonth" into "Month" example
  8. todayDayOfMonthTwoDigits – (02) enter stored value "todayDayOfMonthTwoDigits" into "Month" example
  9. todayDayOfWeek – (Monday) enter stored value "todayDayOfWeek" into "Day" example
  10. todayDayOfWeekShort – (Mon) enter stored value "todayDayOfWeekShort" into "Day" example
  11. nowHour – (2 or 14) select stored value "nowHour" from "Start time" example
  12. nowHourTwoDigits – (02 or 14) select stored value "nowHourTwoDigits" from "Start time" example
  13. nowHourAmPm – (2) select stored value "nowHourAmPm" from "Itinerary" example
  14. nowHourTwoDigitsAmPm – (02) select stored value "nowHourTwoDigitsAmPm" from "Itinerary" example
  15. nowAmPm – (PM) select stored value "nowAmPm" from "Time of day" example
  16. nowMinute – (5) enter stored value "nowMinute" into "Start time" example
  17. nowMinuteTwoDigits – (05) enter stored value "nowMinuteTwoDigits" into "Start time" example
  18. nowSecond – (7) enter stored value "nowSecond" into "Sec" example
  19. nowSecondTwoDigits – (07) enter stored value "nowSecondTwoDigits" into "Sec" example
  20. nowNanosecond – (355881000) save string with parameters "email${nowNanosecond}@testrigor-mail.com" as "newEmail" example
  21. nowDateIso – (2023-09-02) save stored value "nowDateIso" as "currentDate" example
  22. nowTimeIso – (02:05:07) save stored value "nowTimeIso" into "currentTime" example
  23. nowDateTimeIso – (2023-09-02T02:05:07.165068-07:00[America/Los_Angeles]) save stored value "nowDateTimeIso" as "currentDateTime" example
  24. nowMillisecondsFrom1970 – (1637061365335) save stored value "nowMillisecondsFrom1970" as "timeStamp" example
  25. nowDateTimeRFC1123UTC – (Mon, 16 Sep 2023 16:32:41 GMT) save stored value "nowDateTimeRFC1123UTC" as "currentDateTime" example
  26. nowUnixTime – (1637061404) save stored value "nowUnixTime" as "currentUnixTime" example

Note: Time is always generated based on the time zone indicated in the suite settings. In Settings->Advanced, select the desired location from the time zone dropdown.

Use the string with parameters command to put together the current date if the desired format is not already provided:
save string with parameters "${todayMonth} ${todayDayOfMonth}, ${todayYear}" as "todayFullDate"

The above command will produce a the variable todayFullDate with a value that would look like August 15, 2024.

Calendars and Pickers

For selecting dates on most calendars, just use click and/or select:
click "next month" until "calendar" contains stored value "todayMonth"
click stored value "todayDayOfMonthTwoDigits" in the context of stored value "todayMonth"
For selecting dates on a date picker/wheel (usually on mobile), scrolling or swiping may work, but the enter command is usually best:
enter stored value "todayMonth" into "month"
enter stored value "todayDayOfMonth" into "day"
enter stored value "todayYear" into "year"

Getting past and future dates dynamically

Some test cases need to use dates that are different depending on the day the case is executed. We can validate or save dynamic dates as variables with JavaScript ECMAScript 5.1 compatible expressions.

Day of the month

Save the date one day from the current date (a single digit [1] if the date is less than the “10”)

save expression "var d = new Date();d.setDate(d.getDate()+1);''+d.getDate();" as "tomorrow"
Note: Simply modify the +1 in d.setDate(d.getDate()+1) to set the date in the expression to a date a certain number of days before or after the current date. Use + for days in the future and - for days in the past.
Validate that the date two days from the current date is on the screen (double digits [01] if the date is less than the “10”)
check that page contains expression "var d = new Date();d.setDate(d.getDate()+2);('0' + d.getDate()).slice(-2);"

Month of the year

Save the month number 30 days from the current date (a single digit [1] if the month is January to September)
save expression "var d = new Date();d.setDate(d.getDate()+30);''+(d.getMonth()+1)" as "nextMonth"
Save the month number 60 days from the current date (double digits [01] if the month is January to September)
save expression "var d = new Date();d.setDate(d.getDate()+60);('0' + (d.getMonth()+1)).slice(-2)" as "monthAfterNext"
Save what the abbreviated month (Jan) will be in 30 days into a variable.
save value from expression "var ms = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];var tda = new Date();tda.setDate(tda.getDate()+30); ms[tda.getMonth()];" as "shortMonth30Days"
This saves what the full month (January) will be in 60 days into a variable.
save value from expression "var ms = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];var tda = new Date();tda.setDate(tda.getDate()+60); ms[tda.getMonth()];" as "fullMonth60Days"

Year

Save what the 4-digit (2025) year will be in 365 days from the current day.
save expression "var aDate=new Date();aDate.setDate(aDate.getDate()+365);''+aDate.getFullYear()" as "nextYear"
Save what the 2-digit (25) year was 365 days ago from the current day.
save expression "var d = new Date();d.setDate(d.getDate()-365);year = d.getFullYear(); ''+d.getFullYear();year.toString().substr(-2);" as "twoDigitYear"

Full date

Save the full date month first in an all-number format (1/21/2024).
save value from expression "var d = new Date();d.setDate(d.getDate()+1);d.getMonth()+ 1+'/'+d.getDate()+'/'+d.getFullYear()" as "tomorrowMonthDayYear"
Save the full date day first in an all-number format (21/1/2024).

save value from expression "var d = new Date();d.setDate(d.getDate()-1);d.getDate()+ 1+'/'+d.getMonth()+'/'+d.getFullYear()" as "yesterdayDayMonthYear"

Save the full date in an all-number format using double digits for numbers 1-9 (01/21/2024).
save value from expression "var d = new Date();d.setDate(d.getDate()+1);('0' + (d.getMonth()+1)).slice(-2)+'/'+('0' + d.getDate()).slice(-2)+'/'+d.getFullYear()" as "tomorrowMonthDayYearTwoDigits"
Save the full date 30 days from the current date (January 1, 2025).
save value from expression "var ms = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];var tda = new Date();tda.setDate(tda.getDate()+12); ms[tda.getMonth()] + ' ' + [tda.getDate()] +', ' + tda.getFullYear();" as "todayNextMonth"
Save the full date 30 days from the current date with double-digit days between 1 and 9 (January 01, 2025).
save value from expression "var ms = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];var tda = new Date();tda.setDate(tda.getDate()+30); ms[tda.getMonth()] + ' ' + ('0' + tda.getDate()).slice(-2) +', ' + tda.getFullYear();" as "todayNextMonthDoubleDigit"
Save the full date 7 days from the current date with the month abbreviated (Jan 01, 2025).
save value from expression "var ms = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];var tda = new Date();tda.setDate(tda.getDate()+7); ms[tda.getMonth()] + ' ' + ('0' + tda.getDate()).slice(-2) +', ' + tda.getFullYear();" as "nextWeekShortMonth"
Save the date 7 days from the current date as an ordinal number (January 1st, 2025).
save value from expression "var ms = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];var d=['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', '11th', '12th', '13th', '14th', '15th', '16th', '17th', '18th', '19th', '20th', '21st', '22nd', '23rd', '24th', '25th', '26th', '27th', '28th', '29th', '30th', '31st'];var tda = new Date();tda.setDate(tda.getDate()+7); ms[tda.getMonth()] + ' ' + d[tda.getDate()] + ', ' + tda.getFullYear();" as "nextWeekOrdinal"

Test your knowledge

check that page contains string with parameters "${todayMonth} ${todayDayOfMonth}, ${todayYear}"
check that page contains string with parameters "${todayMonth} ${todayDayOfMonthTwoDigits}, ${todayYear}"
check that page contains string with parameters "${todayMonthShort} ${todayDayOfMonth}, ${todayYear}"
check that page contains string with parameters "${todayMonthShort} ${todayDayOfMonthTwoDigits}, ${todayYear}"
save expression "var d = new Date();d.setDate(d.getDate()+4);('0' + d.getDate()).slice(-2);" as "fourDaysLater"
save expression "var d = new Date();d.setDate;d.getDate();''+d.getDate(+4);" as "fourDaysLater"
save expression "var d = new Date();d.setDate(d.getDate()+4);''+d.getDate();" as "fourDaysLater"
save expression "var d = new Date();d.setDate(d.getDate);('0' + d.getDate()).slice(+4);" as "fourDaysLater"