Create Current, Past and Future Dates
DevAssure provides a features called Advanced Code Blocks which allows users to write custom JavaScript code to generate dynamic data. This feature can be used to create current, past, and future dates that can be utilized in various scenarios such as scheduling events, setting deadlines, or validating date-related functionalities during test automation.
- Getting started: /docs/DevAssure/Code%20Blocks/codeblock/
- Use NPM Libraries in code blocks: /docs/DevAssure/Code%20Blocks/npmLibraries/
Function to create the date using a delta parameter
const options = {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: true
};
const currentDate = new Date();
const futureDate = new Date(currentDate);
if (delta != 0) {
futureDate.setDate(Number(currentDate.getDate()) + Number(delta));
}
const formattedFutureDate = futureDate.toLocaleString('en-US', options);
The code above defines a function to create a date based on a delta parameter, which represents the number of days to add or subtract from the current date. Here's a breakdown of how it works:
-
Options Object: The
optionsobject specifies the formatting options for the date, including the month, day, year, hour, minute, and whether to use a 12-hour clock. -
Current Date: The
currentDatevariable holds the current date and time. -
Future Date: The
futureDatevariable is initialized to the current date. If thedeltaparameter is not zero, the code adjusts the day of thefutureDateby adding thedeltavalue. -
Formatted Date: The
formattedFutureDatevariable holds the formatted date string based on the specified options.

Sample DevAssure code
