37

I want to get the current date and time as example date: 11/10/2014 and time 8:30 am or 6:00 pm and pass it as parameters to my Jmeter test. Can some help me do this.

Mark Fisher
  • 9,838
  • 3
  • 32
  • 38
user3920295
  • 889
  • 2
  • 13
  • 31
  • if you asked this question few years ago I'd have jmeter installed and I would answer this. But something along this you should do. 1. Create a beanshell processor and inside execute piece of java code `new Date()` and store it in a variable, and just reuse that variable in your test. – ant Nov 20 '14 at 15:18
  • I tried. I am not very familiar with java but tried ${__BeanShell(new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date()))}. It throws me an error "jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval In file: inline evaluation of: ``11/20/2014 09:25:34 ;'' Encountered "0" at line 1, column 12.". – user3920295 Nov 20 '14 at 15:30
  • @https://stackoverflow.com/users/3920295/user3920295 It would be good for JMeter community if you could accept the answer stackoverflow.com/a/27048364/460802 so that users are confident with right method. Thanks – UBIK LOAD PACK Feb 10 '19 at 21:01
  • Another useful link in this context: https://stackoverflow.com/a/62231892/4398100 – Anshul Singhal Jun 06 '20 at 13:12

7 Answers7

77

Use __time function:

  • ${__time(dd/MM/yyyy,)}

  • ${__time(hh:mm a,)}

Since JMeter 3.3, there are two new functions that let you compute a time:

__timeShift

  "The timeShift function returns a date in the given format with the specified amount of seconds, minutes, hours, days or months added" and 

__RandomDate

  "The RandomDate function returns a random date that lies between the given start date and end date values." 

Since JMeter 4.0:

dateTimeConvert

Convert a date or time from source to target format

If you're looking to learn jmeter correctly, this book will help you.

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
  • Is there any way to add a couple of hours to this value to set the time stamp in the future? – tester Nov 17 '15 at 15:52
  • 1
    To mutate the date, you could try using __javascript instead, e.g., to get a timestamp 6 hours in the future: ${__javaScript(new Date(new Date().setHours(new Date().getHours()+6)).toISOString())} – Sybrand May 18 '16 at 13:14
  • 1
    For those finding this question in the future: JMeter 3.3 has two new functions that let you compute a time: __timeShift - "The timeShift function returns a date in the given format with the specified amount of seconds, minutes, hours, days or months added" and __RandomDate - "The RandomDate function returns a random date that lies between the given start date and end date values." – Steve Feb 08 '18 at 09:04
30

it seems to be the java SimpleDateFormat : http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

here are some tests i did around 11:30pm on the 20th of May 2015

${__time(dd-mmm-yyyy HHmmss)} 20-032-2015 233224
${__time(d-MMM-yyyy hhmmss)}  20-May-2015 113224
${__time(dd-m-yyyy hhmmss)}   20-32-2015 113224
${__time(D-M-yyyy hhmmss)}    140-5-2015 113224
${__time(DD-MM-yyyy)}         140-05-2015
andrew lorien
  • 2,310
  • 1
  • 24
  • 30
  • 1
    Is there any way to add a couple of hours to this value to set the time stamp in the future? – tester Nov 17 '15 at 15:52
  • There is no easy way... but there are two hard ways : – andrew lorien Nov 24 '15 at 01:36
  • 1
    1) use a beanshell interpreter to set a date using java (see the comments on the original question). 2) set the time into one or more variables and use a tedious series of jMeter functions (intNum and split) to generate the time string you want. – andrew lorien Nov 24 '15 at 01:43
  • In which module we need to use this function in jmeter – Rajesh Om Jul 14 '17 at 05:19
  • @RajeshOm in this question it is being used as a "Value" in the "Parameters" tab of the "HTTP Request" module. I often put dates in a "User Defined Variables" module to use later... but it can be used in almost any field in almost any module. – andrew lorien Jul 17 '17 at 04:48
25

JMeter is using java SimpleDateFormat

For UTC with timezone use this

${__time(yyyy-MM-dd'T'hh:mm:ssX)}
MarekM
  • 1,426
  • 17
  • 14
  • 2
    Is there any way to add a couple of hours to this value to set the time stamp in the future? – tester Nov 17 '15 at 15:51
12

Use ${__time(yyyy-MM-dd'T'hh:mm:ss)} to convert time into a particular timeformat. Here are other formats that you can use:

yyyy/MM/dd HH:mm:ss.SSS
yyyy/MM/dd HH:mm:ss
yyyy-MM-dd HH:mm:ss.SSS
yyyy-MM-dd HH:mm:ss
MM/dd/yy HH:mm:ss

You can use Z character to get milliseconds too. For example:

yyyy/MM/dd HH:mm:ssZ => 2017-01-25T10:29:00-0700
yyyy/MM/dd HH:mm:ss.SSS'Z' => 2017-01-25T10:28:49.549Z

Most of the time yyyy/MM/dd HH:mm:ss.SSS'Z' is required in some APIs. It is better to know how to convert time into this format.

S.Mishra
  • 3,394
  • 28
  • 20
6

Actually, for UTC I used Z instead of X, e.g.

${__time(yyyy-MM-dd'T'hh:mm:ssZ)}

which gave me:

2017-09-14T09:24:54-0400
tima
  • 1,498
  • 4
  • 20
  • 28
user8609645
  • 61
  • 1
  • 1
6

Use this format: ${__time(yyyy-MM-dd'T'hh:mm:ss.SS'Z')}

Which will give you: 2018-01-16T08:32:28.75Z

Maximus
  • 625
  • 1
  • 7
  • 10
4

Should have double quotes surrounding the ${}

String todaysDate = "${__time(yyyy-MM-dd'T'HH:mm:ss.SSS'Z')}";
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29