Tuesday, October 23, 2012

Current time formatted in JMeter

There are times when I need to submit a date argument that is relative to the current time. I have put together a javascript one-liner that works directly in the value field. It's ugly, but it works. The function minFromNow(120) will return a formatted date for two hours from now.

Value:
${__javaScript(function pad(digit){if (digit < 10){return '0' + digit;} else return digit;}; function formTime(d){return '' + d.getFullYear() + '-' + pad(d.getMonth()+1) + '-' + pad(d.getDate()) + ' ' + pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds());}; function minFromNow(mi){var dt=new Date(); dt.setMinutes(dt.getMinutes() + mi); return formTime(dt)}; minFromNow(0),Dummy)} 

Output:
2012-10-23 19:05:42