Catalog client script - How to set the time of a Date/Time variable

KoenHaemels
Tera Contributor

Hi

I'm trying to set the time of a date/time variable in a catalog client script.
However, the date gets changed and set. But the time remains on 00:00:00.

var dateNumber = getDateFromFormat(g_form.getValue('temp1'), g_user_date_format);
var date = new Date(dateNumber);

// Initialize a new Date object
var newDateTime = new Date(date.getFullYear(), date.getMonth(), date.getDate());

// Set the time to 23:59:59
newDateTime.setHours(23);
newDateTime.setMinutes(59);
newDateTime.setSeconds(59);

// Set the value in the datetimeField
g_form.setDisplayValue('temp2', newDateTime);


Temp1 is a date variable and temp2 is a date/time variable.
The script gets the date that is selected in temp1 and puts it in temp2 and should add the time 23:59:59.
However the date is set but the time is not.

Regards

15 REPLIES 15

Saloni Suthar
Mega Sage
Mega Sage

Please try to use GlideDateTime instead of Date

var newDateTime = new GlideDateTime(date.getFullYear(), date.getMonth(), date.getDate());

If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @KoenHaemels 

 

https://www.servicenow.com/community/developer-forum/how-to-set-date-time-field-value-to-a-specified...


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

saurabh_dubey
Kilo Sage

Hi @KoenHaemels ,

 

Use Glide Date time as it is a server side api, so you have to use glideAjax in this case. 

May I know your problem statement that you are trying to solve.

 

If my solution helps you please mark my solution helpful.

 

Thanks and regards,

Saurabh Dubey.

Community Alums
Not applicable

Hi @KoenHaemels ,

 

Just replace Date in your code with GlideDateTime 

This will fix the issue & help you populate time as well along with date.

 

Please mark it as "Accept as Solution" and "Helpful". if this solution helped you.

 

Thanks & Regards,
Madhan Somesh.