Catalog client script - How to set the time of a Date/Time variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2024 08:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-18-2024 11:44 PM
@saurabh_dubey thank you for the code. But i would like to keep in the catalog item itself under the tab "Catalog Client Script". So the code needs to be merged to one, i guess?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-18-2024 11:46 PM
Yes @KoenHaemels, it will work in Catalog client script. just use it there
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2024 12:15 AM
I would like to keep it in the catalog item.
I've changed it to this, is this the correct way? Because i receive an error.
function onCondition() {
var currentDate = g_form.getValue('temp1');
var ga = new GlideAjax();
ga.addParam('sysparm_name', addDate);
ga.addParam('sysparm_temp1', currentDate);
ga.getXMLAnswer(getResponse);
function getResponse(response){
alert(response);
if(response){
g_form.setValue('temp2',response);
}
}
function addDate(){
var currentDate = this.getParameter("sysparm_temp1");
var gTime = new GlideDateTime(currentDate + " 23:59:59");
return gTime.getValue();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2024 12:23 AM
My simple suggestion, learn a bit about glideajax, you clearly don't know this. Also if possible may I know where are you writing the code, I think you are using UI policies and I clearly mentioned onChange client script.
Get some basics @KoenHaemels, the above code you have written will not work.
If you found my reply helpful please mark it as helpful
Thanks and regards
Saurabh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2024 01:49 AM
I used it by mistake for a quick test.
I'm on a tight schedule to finish that item so i don't have time to quickly learn basics. It would be very helpful you can help me with this.