How to get current date?

Problem

You want to get the current date in yyyy-mm-dd format

Solution

We get the current date using javascript.

Sample Code

js begin
function formatDate(date) {
    var d = new Date(date),
        mm = '' + (d.getMonth() + 1),
        dd = '' + d.getDate(),
        yy = d.getFullYear();
    return [yy.toString(), (mm>9?'':'0')+mm, (dd>9?'':'0')+dd].join('-');
}
today = formatDate(new Date());
js finish

echo "today = "+today

Output

If the current date is Aug 31, 2018, then you will see the following in the command window:

today = 2018-08-31

Explanation

Line 1: Start of the javascript codes.
Lines 2-7: return the current date in yyyy-mm-dd format.
Line 7: I'm using '-' to join the year, month and date.
.join('-')
You can change this to use other character, such as '_'. If you like to display the date as yyyymmdd, then put an empty string here ''
Line 9: Store today's date in the variable today
Line 10: End of the javascript codes.
Line 12: Variables that are set using javascript can be accessed in TagUI.

Variations

  1. To display date in yy-mm-dd format, e.g. 18-08-31
  2. js begin
    function formatDate(date) {
        var d = new Date(date),
            mm = '' + (d.getMonth() + 1),
            dd = '' + d.getDate(),
            yy = d.getFullYear();
        return [yy.toString().substr(2,2), (mm>9?'':'0')+mm, (dd>9?'':'0')+dd].join('-');
    }
    today = formatDate(new Date());
    js finish
    
    echo "today = "+today
    

Related Articles

Other Resources

TagUI Tutorials

Questions / Comments

If you have any questions or comments on the codes above, you can leave your comments below, or post your questions in the RPA Community Forum.

Comments (0)

There are no comments posted here yet

Leave your comments

Posting comment as a guest. Sign up or login to your account.
Attachments (0 / 3)
Share Your Location