- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2023 02:15 PM
I have what looks to be a simple client script that is passing a date to script include though GlideAjax.
I pass the date into a GlideDateTime and then return the getDisplayValue() to the Client Script.
In the Client script I alert the date, but the date is now different from the one I collected from the form. I did not manipulate the date at all why is it different returning from the script include?
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// if (oldValue == '') {
var newDate = g_form.getValue('adjusted_deployment_start');
alert(newValue);
alert(newDate);
var ga = new GlideAjax('ApacClientHelper');
ga.addParam('sysparm_name', 'calcAdjustedDeploymentEnd');
ga.addParam('sysparm_date', newDate);
ga.getXMLAnswer(updateEndDate);
// }
}
function updateEndDate(answer) {
var returnedDate = answer;
alert(answer);
}
Script Include
calcAdjustedDeploymentEnd: function() {
var gdt = new GlideDateTime(this.getParameter('sysparm_date'));
return gdt.getDisplayValue();
},
The first two alerts on the client script show the correct time entered into the field
The Third alert after the script include is not the same time anymore
Additional Details: The time has been adjusted by -7 hours, and that is our timezone UTC-7. And our system default timezone is UTC-7 as well.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2023 09:45 AM - edited 11-09-2023 09:48 AM
Hi @Calvert
So let's try these variables g_user_date_time_format and g_user_date_format.
var resolved_date_time = getDateFromFormat(g_form.getValue('resolved_at'), g_user_date_time_format);
var objResolvedDate = new Date(resolved_date_time);
objResolvedDate.setHours(objResolvedDate.getHours() + 3); //+3 hours
var new_resolved_date_time = formatDate(objResolvedDate, g_user_date_time_format);
You can also convert to system format and pass to the Script Include to calculate.
var resolved_date_time = getDateFromFormat(g_form.getValue('resolved_at'), g_user_date_time_format);
var objResolvedDate = new Date(resolved_date_time);
var new_resolved_date_time = formatDate(objResolvedDate, "yyyy-MM-dd hh:mm:ss");
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2023 09:09 PM
Hi @Calvert
You're seems messing up between value and display value in the date time field.
What's happening from your script is that.
1. You get a date time from a specific time zone. => date_time_1
2. Then you initiate that time at UTC => GlideDateTime(date_time_1)
3. And convert it again to your current user's time zone. => gdt.getDisplayValue() => now it becomes date_time_2
Sample. (As you can see it becomes 07:00:00 from 06:00:00 (original)
So in your function from the script include, just simply return gdt or gdt.getValue().
Sample below.
calcAdjustedDeploymentEnd: function() {
var gdt = new GlideDateTime(this.getParameter('sysparm_date'));
gdt.addSeconds(3*60*60); //add 3 hours as your requirement
return gdt.getValue(); //or gdt
},
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2023 07:09 AM
getValue() returns in a date format that they system doesn't accept
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2023 09:45 AM - edited 11-09-2023 09:48 AM
Hi @Calvert
So let's try these variables g_user_date_time_format and g_user_date_format.
var resolved_date_time = getDateFromFormat(g_form.getValue('resolved_at'), g_user_date_time_format);
var objResolvedDate = new Date(resolved_date_time);
objResolvedDate.setHours(objResolvedDate.getHours() + 3); //+3 hours
var new_resolved_date_time = formatDate(objResolvedDate, g_user_date_time_format);
You can also convert to system format and pass to the Script Include to calculate.
var resolved_date_time = getDateFromFormat(g_form.getValue('resolved_at'), g_user_date_time_format);
var objResolvedDate = new Date(resolved_date_time);
var new_resolved_date_time = formatDate(objResolvedDate, "yyyy-MM-dd hh:mm:ss");
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2023 11:04 AM
Thank you! This worked for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2023 09:16 PM
what's your requirement here?
you are simply sending a date/time value and again returning date/time value without any manipulation?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader