The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Setting Date-Time fields on forms

Jamsta1912
Giga Guru

Hi all,

 

I'm trying to use a client script to set a value in date-time field, so that it's 1 hour in the future (from right now). It feels like this should be easy!... but I'm struggling to find a way to manipulate date-times in a way that allows me to do this in a client script. Any advice, much appreciated.

 

Thank you.

1 ACCEPTED SOLUTION

andrew_venables
ServiceNow Employee
ServiceNow Employee

Yeah this can be a bit tricky but there are a few undocumented variables that allow you to do this in a non datetime format specific way:



var dtNow = new Date();


var dtNowStr = formatDate(dtNow, g_user_date_time_format);


var dtNowNum = getDateFromFormat(dtNowStr, g_user_date_time_format);


var oneHour = 60*60*1000; // in ms


var dtNewNum = dtNowNum + oneHour;


var dtNew = new Date(dtNewNum);


var dtNewStr = formatDate(dtNew, g_user_date_time_format);


g_form.setValue('start_date', dtNewStr);



Which should be good for all timezones and all date formats


View solution in original post

3 REPLIES 3

andrew_venables
ServiceNow Employee
ServiceNow Employee

Yeah this can be a bit tricky but there are a few undocumented variables that allow you to do this in a non datetime format specific way:



var dtNow = new Date();


var dtNowStr = formatDate(dtNow, g_user_date_time_format);


var dtNowNum = getDateFromFormat(dtNowStr, g_user_date_time_format);


var oneHour = 60*60*1000; // in ms


var dtNewNum = dtNowNum + oneHour;


var dtNew = new Date(dtNewNum);


var dtNewStr = formatDate(dtNew, g_user_date_time_format);


g_form.setValue('start_date', dtNewStr);



Which should be good for all timezones and all date formats


Thank you Andrew. I've got this working - This is spot on!... and I know I'll find this useful elsewhere too.



Regards


Jamsta.


rajinis
Kilo Guru

Where can I find the following functions? I don't have these and my code is not working, it does not return anything.

formatDate(dtNow, g_user_date_time_format);
getDateFromFormat(dtNowStr, g_user_date_time_format);