CheatSheet for JMeter __time Function Calls

Often I have to deal with dates and times in my scripting and luckily JMeter is quite good at dealing with them. The function to call is __time (JMeter doco http://jmeter.apache.org/usermanual/functions.html). The thing is the doco is pretty sketchy about all the different formats it can take. So I just created a test script to see what works. The examples are below ready to copy & paste. I also have included scripts for time and date manipulation for completeness.

Note that __time can only display the current time local to the machine.

Function Example Result for
01/12/2015 02:00PM
${__time(YMD)} 20151201
${__time(yyyyMMdd)} 20151201
${__time(yyMMdd)} 151201
${__time(dd-MM-yyyy)} 01-12-2015
${__time(dd/MM/yyyy hh:mm:ss)} 01/12/2015 14:00:00
${__time(dd/MM/yyyy HH:mm:ss a)} 01/12/2015 02:00:00 PM
${__time()} 1454358328739
${__time(yyyy-MM-dd’T’hh:mm:ssX)} 2015-12-01T14:00:00+13

There is no possibility to add/subtract in the __time function. In order to do that you will need to use BeanShell, groovy or javascript.

Here is a simple groovy (JSR223 Processor) example to add a day to the current date. There are dozns of other ways to do this though.

import java.text.SimpleDateFormat; 
import java.util.Date; 

// You need to define ${InDays} as how many days forward/back you want to go
int InDays = Integer.valueOf(vars.get("InDays"));

Date date = new Date(); 
date.setDate(date.getDate() + InDays); 
SimpleDateFormat df  = new SimpleDateFormat("dd/MM/yyyy"); 
String formattedDate = df.format(date); 

// Resulting date will be in ${FutureDate}
vars.put("FutureDate",formattedDate);

This function is only good for adding/subtracting days though. If you need to manipulate time the below function can be used. The below can also be changed to add days too though. It would be overkill if you just need to manipulate the date though.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

// The below variables need to be defined
int ForwardSecond     = Integer.valueOf(vars.get("ForwardSecond"));
int ForwardMinute     = Integer.valueOf(vars.get("ForwardMinute"));
int ForwardHour       = Integer.valueOf(vars.get("ForwardHour"));

Date now         = new Date();
Calendar c       = Calendar.getInstance();
c.setTime(now); 
c.add(Calendar.SECOND, ForwardSecond);
c.add(Calendar.MINUTE, ForwardMinute);
c.add(Calendar.HOUR,   ForwardHour);
Date NewTime        = c.getTime();
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); 
String mytime       = df.format(NewTime);

// Rsult can be accessed through ${NewTime}
vars.put("NewTime",mytime);

I hope this helps those needing to deal with times and dates in JMeter.

by Oliver Erlewein

5 thoughts on “CheatSheet for JMeter __time Function Calls

  1. Pingback: Testing Bits – 1/31/16 – 2/6/16 | Testing Curator Blog

  2. Could you verify that ${__time(yyyy-MM-dd’T’hh:mm:ssX)} should be ${__time(yyyy-MM-dd’T’HH:mm:ssX)} to return the hours in military time?

  3. Hello, how can I get a different time when the Thread Group Loop Count increase?

    Say I have a User Defined Variable currentTime=${__time(dd/MM/yyyy hh:mm:ss)}
    Then inside a BeanShell Sampler request, I get the value by newCurrentTime = vars.get(“currentTime”);
    This will get me the datetime, but in the test plan I have added few seconds delay between Loop Count, but next request still have the same value.

    Thx

    • Hi Aeda,
      Not quite sure what you are doing there. Why don’t you just get the time withing your BeanShell i.e. in code? Why do you need it from JMeter?
      Oliver

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s