skip to Main Content

I wanted to create a dynamic date variable in JMeter. Following responses in Jmeter – get current date and time
I found that the following works:

${__javaScript(new Date(new Date().setMonth(new Date().getMonth()+6)).toISOString())}

It sets a value of this variable 6 months in the future. Perfect.

The problem is that the formatting I need this date to be is:
2024-03-21T10:10:10

But the above line results in:
2024-03-21T20:54:55.386Z

I need to get rid of the last few characters but I’m not sure how to handle that on one line.

Using substring or slice would be ideal but it doesn’t work. The result is blank. I’m guessing that I’m using a wrong syntax. Thinking I can’t use substring on a date, I tried to wrap it into a new String but that doesn’t fix it either.

For example,

${__javaScript(new String(new Date(new Date().setMonth(new Date().getMonth()+6)).toISOString()).substring(0,18))}

Ideas how to fix this?

As a side note in case if anyone wants to suggest timeShift JMeter function, I can’t upgrade my JMeter version just yet because of other reasons so I need to use what works in version 2.13.
I’m hoping that there’s a way to accomplish this by other means, for example with JavaScript.

4

Answers


  1. I don’t know anything about jmeter, but will this work?

    ${__javaScript(new Date(Date.now() + 182 * 24 * 60 * 60 * 1000).toISOString().substring(0, 18)}
    
    Login or Signup to reply.
  2. try this

    const date = new Date();
    const isoDate = date.toISOString();
    const dateOnly = isoDate.substring(0, 10);
    
    console.log(dateOnly); // e.g., Outputs: 2023-09-21
    
    Login or Signup to reply.
  3. Just use split and join:

    var date = new Date()
    var dateString = date.toISOString()
    var shortDate = dateString.split('.')[0]
    
    console.log(shortDate) // 2023-09-21T22:29:56
    
    Login or Signup to reply.
  4. Seems you want to have the time in your local timezone, so correct with getTimezoneOffset():

    console.log(new Date(new Date(new Date().setMonth(new Date().getMonth() + 6)).setMinutes(new Date().getMinutes() - new Date().getTimezoneOffset())).toISOString().slice(0, -5))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search