LoadRunner Date/Time functions

LoadRunner stores date/time parameters locally on the LoadRunner PC. This means that opening a script on another users PC, where the date/time parameter is not stored, can cause corruption to the date/time parameters in a script. You know that this has happened when a date/time parameter in a format similar to this [%Y%m%d%H%M%S] is sent to the server rather than the format that you intended.

Due to this problem, we recommend that the lr_save_datetime function is used to save date or time values to a string. The string can then be used in the script and the script is portable between PCs because the script is no longer relying on the locally stored parameters.

This sample code shows different lr_save_datetime functions which can be incorporated into your scripts. As usual this sample code can be downloaded for further examination.


lr_save_datetime(“%d/%m/%y”, DATE_NOW, “DDMMYY”);
lr_output_message(“Today’s Date is %s”,lr_eval_string(“{DDMMYY}”));

lr_save_datetime(“%d/%m/%Y”, DATE_NOW, “DDMMYYYY”);
lr_output_message(“Today’s Date is %s”,lr_eval_string(“{DDMMYYYY}”));

lr_save_datetime(“%B %d %Y”, DATE_NOW, “Today”);
lr_output_message(“Today is %s”,lr_eval_string(“{Today}”));

lr_save_datetime(“%B %d %Y”, DATE_NOW + ONE_DAY, “Tomorrow”);
lr_output_message(“Tomorrow is %s”,lr_eval_string(“{Tomorrow}”));

lr_save_datetime(“%A”, DATE_NOW – ONE_DAY, “Yesterday”);
lr_output_message(“Yesterday was %s”,lr_eval_string(“{Yesterday}”));

lr_save_datetime(“%A”, DATE_NOW, “Today”);
lr_output_message(“Today is %s”,lr_eval_string(“{Today}”));

lr_save_datetime(“%A”, DATE_NOW + ONE_DAY, “Tomorrow”);
lr_output_message(“Tomorrow is %s”,lr_eval_string(“{Tomorrow}”));

lr_save_datetime(“%X”, TIME_NOW , “Time”);
lr_output_message(“The time is %s (in this locale’s date format)”,lr_eval_string(“{Time}”));

lr_save_datetime(“%X”, TIME_NOW + ONE_HOUR , “Time”);
lr_output_message(“In one hour it will be %s”,lr_eval_string(“{Time}”));

lr_save_datetime(“%A”, DATE_NOW + ONE_DAY, “Tomorrow”);
lr_output_message(“Tomorrow is %s”,lr_eval_string(“{Tomorrow}”));

lr_save_datetime(“%j”, DATE_NOW, “Today”);
lr_output_message(“Today is day %s in the year”,lr_eval_string(“{Today}”));

lr_save_datetime(“%Z”, DATE_NOW, “TimeZone”);
lr_output_message(“This machine is in the ‘%s’ time zone”,lr_eval_string(“{TimeZone}”));

lr_save_datetime(“%p”, DATE_NOW, “AMPM”);
if (strcmp (lr_eval_string(“{AMPM}”),”PM”)!=0)
lr_output_message(“Good Morning”);
lr_output_message(“Good Afternoon”);

Prevent a LoadRunner script from running between two times

Some scripts cannot be run between certain times, this is especially the case when we are running long duration tests or preparing test data by data-seeding overnight.

In these cases it may be useful to specify times when you don’t want a script to run, for example you might want to schedule a script so that it can’t run when backups or system restarts are taking place.

The attached script contains an example of a function (CheckExclusionTime) which can prevent a script from running between certain times. The script is made more complicated by the fact that you might want to prevent a script from running between 23:50 one day and 00:15 the next day. To achieve the desired result it is necessary to convert any time into an integer value representing the number of minutes which have passed from 00:00 on Monday until the current time and then calculating if the current time falls within the excluded period.

2016-10-06-12_45_30-timeexclusion-hp-virtual-user-generator-web-http_htmlThe integers 1 through to 7 represent the days Monday through to Sunday.

Other integers are used to represent the exclusion start time and exclusion end time.

Sample LoadRunner scripts, demonstrating LOOPs

I have found numerous occasions where I need to use a LOOP within a LoadRunner script to repeatedly perform an action. There are several ways that LOOPs can be used to repeat actions for either a specified number of times or until a particular occurrence is observed.


This script includes an example of FOR, WHILE and DO LOOPs.

Click here to download the script.