How to Get Today’s Date in Selenium IDE

Get today’s date in Selenium IDE and format it like a boss. This tutorial will show you how, with code snippets you can reuse and screenshots.

Published Categorized as Web & Code

Suppose you’re creating a test case in Selenium IDE, and you need today’s date for one of the steps. What’s the best way to get the date, and how to get it in the format you need?

To make a long story short, to get today’s date in Selenium IDE, you have to add an “execute script” command to the control flow for your test case and use the JavaScript date constructor.

This tutorial will show you how to do that, with JavaScript code snippets that you can copy and paste into Selenium IDE.

Getting Today’s Date in Selenium IDE

In DD/MM/YYYY format:

To get today’s date in DD/MM/YYYY format in Selenium IDE, add an “execute script” command to the control flow of your test case and paste the following JavaScript code snippet into the “Target” field:

// Get date in DD/MM/YYYY format
return new Date().toLocaleString('en-GB').split(',')[0]

Finally, enter a name for your variable in the command’s “Value” filter (for example, todaysDate). This is the variable that you will be referencing whenever you need the date.

Here’s what this looks like in Selenium IDE:

Getting today’s date in Selenium IDE

See what we did there?

This test case goes to the base URL, executes a script to construct today’s date in DD/MM/YYYY format, and stores it the todaysDate variable. Then, it references the variable and gets the value of today’s date to type it into a form and submit that form by clicking the button. Finally, it closes the browser window.

In M/D/YYYY format:

To get today’s date in M/D/YYYY format with single-digit dates not prefixed by a zero (January 1, 2022 would be 1/1/2022), add an “execute script” command, paste the following code snippet into the “Target” field, and enter any name for your date variable in the “Value” field:

// // Get date in M/D/YYYY format
return new Date().toLocaleString('en-US').split(',')[0]

In MM/DD/YYYY:

To get today’s date in MM/DD/YYYY format with single-digit dates prefixed by a zero (January 1, 2022 would be 01/01/2022), add an “execute script” command, paste this JavaScript code snippet into the “Target” field, and give your variable any name in the “Value” field:

// // Get date in MM/DD/YYYY format, with single-digit dates prefixed by a zero
return new Date().toLocaleString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit' }).split(',')[0]

Replacing Forward Slashes (/) With Dashes (-)

There may be instances in which you want to have a date with dashes instead of forward slashes (for example, 1-1-2022 instead of 1/1/2022).

To do this, simply add the following method to the end of your code snippet:

.replace(/\//g, "-")

For example, the code snippet below will return a date in M-M-YYYY format instead of M/M/YYYY format:

// Get date in M-D-YYYY format
return new Date().toLocaleString('en-US').split(',')[0].replace(/\//g, "-")

Bottom Line

And there you have it! Now that you know how to do it, getting today’s date in Selenium IDE will be as easy as one, two, three.

Just remember to add the “execute script” command above any form or field where you might need it. And make sure you’re using the code for the format you need and you’re referencing the name of your date variable correctly.

If you have any questions or you want to share some tips of your own with the rest of this post’s readers, leave a reply below!

By Dim Nikov

Editor of Maker's Aid. Part programmer, part marketer. Making things on the web and helping others do the same since the 2000s. Yes, I had Friendster and Myspace.

Leave a comment

Your email address will not be published. Required fields are marked *