JMeter – Timestamp

The generation of the current timestamp is a common requirement in performance testing. Either you need to pass the current timestamp in epoch format or ISO format. JMeter has some time-related functions in the function library which meet the requirement to generate the current, past or future timestamp using offset.

__time() function in JMeter

This function is used to generate the current timestamp in different formats. __time() has two arguments i.e. Time Format and Name of Variable and both are optional. __time() function without any argument returns the current time in epoch format.

FunctionExampleDescription
${__time()}1603188484750Current Time in Epoch Format in milliseconds
${__time(/1000)}1603188484Current Time in Epoch Format in seconds
${__time(yyyy)}2020Current Year
${__time(MM)}10Current Month in digit
${__time(MMM)}Oct3 letters of the Current Month
${__time(MMMM)}OctoberFull spell of Current Month
${__time(d)} / ${__time(dd)}1 / 01
or
21 / 21
Current Day in one or two digits
${__time(h)} / ${__time(hh)}1 / 01
or
11 / 23
Current Hour in one or two digits in 12-hour format (1-12)
${__time(H)} / ${__time(HH)}1 / 01
or
11 / 23
Current Hour in one or two digits in 24-hour format (0-23)
${__time(k)} / ${__time(kk)}1 / 01
or
11 / 23
Current Hour in one or two digits in 24-hour format (1-24)
${__time(K)} / ${__time(KK)}1 / 01
or
11 / 23
Current Hour in one or two digits in 12-hour format (0-11)
${__time(m)} / ${__time(mm)}5/ 05
or
15 / 15
Current Minute in one or two digits
${__time(s)} / ${__time(ss)}2/ 02
or
19 / 19
Current Second in one or two digits
${__time(S)} / ${__time(SS)} /
${__time(SSS)}
3/ 03 / 003
or
13 / 13 / 013
or
310 / 310 / 310
Current Millisecond in one, two or three digits
${__time(a)}AM or PMCurrent Meridian
${__time(z)}ISTTimezone
${__time(Z)}+0530Timezone offset in hours (RFC pattern)
${__time(XXX)}+05:30Timezone offset in ISO format
${__time(u)}3Current Day Number (1 – Monday, 2 – Tuesday, 3 – Wednesday, 4 – Thursday, 5 – Friday, 6 – Saturday, 7 – Sunday )
${__time(E)} / ${__time(EEEE)}Wed / WednesdayCurrent Day in letters
${__time(D)}295Current day count in the year
${__time(W)}4Week in a month (1 – First week, 2 – Second week)
${__time(w)}43Week in a year
${__time(G)}ADCurrent Era
Table 01

If you want to pass the current timestamp (in epoch format) in the request then you can directly use ${__time()} else you can use the above-mentioned argument and generate the value in desired time format.

Example: ${__time(yyyy-MM-dd-HH:mm:ss:SSS a XXX)} will generate 2020-10-21-11:16:38:966 AM +05:30

In case you want to save the value in a variable then add a variable name next to the format separated by a comma.

${__time(yy-MM-dd-HH:mm:ss:SSS, cTime)}

Use ${cTime} variable wherever you want to pass the generated value.

__timeShift() function in JMeter

_timeShift() function is used to generate the past or future date during the performance test. It has five input arguments, among them 2 are optional.

  • Time Format: Refer to the above table (Table 01) and use the argument to create the time format string. e.g. yy-MM-dd
  • Date to Shift: By default, it uses the current timestamp. In case you want to shift the date/time from a particular date/time then specify it. e.g. 2020-10-20
  • How much shift: Specify the amount of day/time to shift
    • P denotes Parse for a future date
    • -P denotes Parse for a past date
    • PT denotes Parse for a future time
    • -PT denotes Parse for a past time
    • D denotes Day
    • H denotes Hour
    • M denotes Minute
    • S denotes Second
  • String Format (Optional)
  • Name of variable (Optional)
JMeter - Timestamp
Figure 01: timeShift function in JMeter

Example:

  1. ${__timeShift(yyyy-MM-dd HH:mm:ss,,P2D,,)} will generate a future date by adding 2 days in the current day
  2. ${__timeShift(yyyy-MM-dd HH:mm:ss,,PT2H,,)} will generate a future time by adding 2 hours in the current time
  3. ${__timeShift(yyyy-MM-dd HH:mm:ss,,PT2M,,)} will generate a future time by adding 2 minutes in the current time
  4. ${__timeShift(yyyy-MM-dd HH:mm:ss,,P2DT2H,,)} will generate a future date and time by adding 2 days and 2 hours in the current timestamp
  5. ${__timeShift(yyyy-MM-dd HH:mm:ss,,-PT2M,,)} will generate a past time by reducing 2 minutes in the current time
  6. ${__timeShift(yyyy-MM-dd HH:mm:ss,,-P2DT2H,,)} will generate a past date and time by reducing 2 days and 2 hours in the current timestamp
  7. ${__timeShift(yyyy-MM-dd HH:mm:ss,2020-10-20,P2D,,)} will generate 2020-10-22 which is the next 2nd day from the specified date.

You may be interested:


Leave a Comment