How to get current date and time?


You want to get the current date and time in yyyy-mm-dd_hh:min format


We get the current date and time using javascript.

Sample Code

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

echo "current date/time = "+now


If the current date and time is Aug 31, 2018 10:08pm, then you will see the following in the command window:

today = 2018-08-31_20:08


Line 1: Start of the javascript codes.
Lines 2-9: return the current date and time in "yyyy-mm-dd_hh:min" format.
Line 9: I'm using '-' to join the year, month and date.
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 ''.

If you're using this as filename, then most operating systems do not allow :. You will want to change this to something like:
return [yy.toString(), (mm>9?'':'0')+mm, (dd>9?'':'0')+dd].join('') + "_"+ (hh>9?'':'0')+hh+(min>9?'':'0')+min;
This will return the following as output:
today = 20180831_2008
If you only want the year displayed as 2 digits only e.g. 180831_2008, then use:
return [yy.toString().substr(2,2), (mm>9?'':'0')+mm, (dd>9?'':'0')+dd].join('') + "_"+ (hh>9?'':'0')+hh+(min>9?'':'0')+min;
Line 11: Store today's date in the variable now
Line 12: End of the javascript codes.
Line 14: Variables that are set using javascript can be accessed in TagUI.

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