Examples on Date and Time in LoadRunner

Following are some examples in Datetime Format:

Datetime Format Codes


The following format codes are supported for lr_save_datetime.


Code
Description
%a
day of week, using locale's abbreviated weekday names
%A
day of week, using locale's full weekday names
%b
month, using locale's abbreviated month names
%B
month, using locale's full month names
%c
date and time as %x %X
%d
day of month (01-31)
%H
hour (00-23)
%I
hour (00-12)
%j
number of day in year (001-366)
%m
month number (01-12)
%M
minute (00-59)
%p
locale's equivalent of AM or PM, whichever is appropriate
%S
seconds (00-59)
%U
week number of year (01-52), Sunday is the first day of the week. Week number 01 is the first week with four or more January days in it.
%w
day of week; Sunday is day 0
%W
week number of year (01-52), Monday is the first day of the week. Week number 01 is the first week with four or more January days in it.
%x
date, using locale's date format
%X
time, using locale's time format
%y
year within century (00-99)
%Y
year, including century (for example, 1988)
%Z
time zone abbreviation
%%
to include the "%" character in your output string


Examples:

Action()
{
    //Variable declaration for future date 
    int iDays = 1, iMonthCurrent, iMonthChanged = 0, iMonth, bDone = 0,bMonthChanged;
  
    //Print Today's date
lr_save_datetime ("%m/%d/%Y"DATE_NOW"pDate1");
lr_output_message ("Today's Date is %s"lr_eval_string ("{pDate1}"));

 //Print Tomorrow's Date in Month Date, Year
lr_save_datetime ("%B %d, %Y"DATE_NOW+ONE_DAY"pDate2");
lr_output_message ("Tomorrow’s Date is %s"lr_eval_string ("{pDate2}"));

 //Print Date/time 5 days and 30 minutes ago in day of week-month-day of month-year and time
lr_save_datetime("%a-%b-%d-%Y %H:%M:%S"DATE_NOW-(ONE_DAY*5)-(ONE_MIN*30), "pDate3");
lr_output_message ("Date/Time 5 days and 30 minutes ago is %s"lr_eval_string ("{pDate3}"));

  //Print Date/time 5 days and 30 minutes ago in day of week-month-day of month-year and time
lr_save_datetime("%a-%b-%d-%Y %H:%M:%S"DATE_NOW+(ONE_DAY*5)+(ONE_MIN*45), "pDate3");
lr_output_message ("Date/Time of next 5 days and 45 minutes is %s"lr_eval_string ("{pDate3}"));

 //Print Day of Month
lr_save_datetime ("%d"DATE_NOW"pDate4");
lr_output_message("Day of Month : %s ",lr_eval_string("{pDate4}"));

lr_save_datetime ("%m/01/%Y"DATE_NOW-(ONE_DAY*(atoi(lr_eval_string ("{pDate4}"))+1)), "pDate5");
lr_output_message ("First Day of Previous Month is %s"lr_eval_string ("{pDate5}"));

// Find the last day of next month
// Not efficient, but a different use of lr_save_datetime
// Keep adding days until month changes 2 times, then back off 1 day

lr_save_datetime ("%m"DATE_NOW"pDate6");
iMonthCurrent = atoi (lr_eval_string ("{pDate6}"));

while (bDone == 0)
{
    lr_save_datetime ("%m"DATE_NOW+(ONE_DAY*(iDays)), "pDate7");
    iMonth = atoi (lr_eval_string ("{pDate7}"));
    if (iMonth != iMonthCurrent)
    {
        bMonthChanged++;
        iMonthCurrent = iMonth;
    }

    if (bMonthChanged == 2)
    {
        bDone = 1;
    }
    else
    {
        iDays++;
    }
}

lr_save_datetime ("%m/%d/%Y"DATE_NOW+(ONE_DAY*(iDays-1)), "pDate8");
lr_output_message ("Last Day of Next Month is %s"lr_eval_string ("{pDate8}"));
  
    return 0;
}

Output:

Starting action Action.
Action.c(8): Today's Date is 03/26/2014
Action.c(12): Tomorrow’s Date is March 27, 2014
Action.c(16): Date/Time 5 days and 30 minutes ago is Fri-Mar-21-2014 11:53:40
Action.c(20): Date/Time of next 5 days and 45 minutes is Mon-Mar-31-2014 13:08:40
Action.c(24): Day of Month : 26 
Action.c(27): First Day of Previous Month is 02/01/2014
Action.c(57): Last Day of Next Month is 04/30/2014
Ending action Action.


Comments

  1. I need to create a parameter which picks up current date in GMT format (my system has IST dates). Is there any way to do it.
    I do not want to use lr_save_datetime function as it sends a static date throughout the script and I have timestamps multiple times in the script.

    Thanks in advance!

    ReplyDelete
    Replies
    1. Select Current date from Parameter file Date/Time and choose required current date format

      Delete
  2. How to get a current week starting date?

    ReplyDelete
  3. For next year

    lr_save_datetime("%Y", DATE_NOW+(ONE_DAY*365), "NextYear");
    lr_output_message ("Next Year is %s", lr_eval_string ("{NextYear}"));

    ReplyDelete
  4. How to get month format as 8 instead of 08

    ReplyDelete
  5. I need to select current time & then for every next record add 10 mins to the current time...how do I do it

    ReplyDelete

Post a Comment

Popular posts from this blog

How to use a value between two different threads in Jmeter

Steps to Analyze AWR Report in Oracle

Correlation and checking in Results