Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to Get the current date time using a client script.

Shraddha desai
Tera Contributor

Hello

How to Get the current date time using a client script.

17 REPLIES 17

@Shraddha desai 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

This was very helpful and solved our problem passing current date & time to a UI action triggered subflow

 

Thank You

Raghu Ram Y
Kilo Sage

Hi @Shraddha desai 

It is not best practice to use server side calls using client script, so I have done it through glide ajax. 

Use the below code, Tested and Working fine.

 

Client Script

function onLoad() {
    var ga = new GlideAjax('MyDateTimeAjax');
    ga.addParam('sysparm_name', 'nowDateTime');
    ga.getXML(SettingDate);

    function SettingDate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('give_fieldname_here', answer);   
    }
}

 

Script Include

var MyDateTimeAjax = Class.create();
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    nowDateTime: function() {
        return gs.nowDateTime();
    }
});

I hope it helps, if so please mark it as both helpful and correct.

@Shraddha Desai have you gone through my suggestion?

if you want only date then just replace the below line in the script include in the above code.

  return gs.now();

I hope it helps, if so please mark it as both helpful and correct.

Hi @Shraddha Desai Thank you for marking it as helpful, if it helps then please mark it as correct or Let me know still if you are having any queries.