- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2024 01:03 PM
- start_date (glide_date_time)
- u_planned_duration (glide_duration)
- end_date (glide_date_time)
addDurationToDateTime: function(start_date, duration) {
/*
Client-Side Example:
var ga = new GlideAjax('addDurationToDateTime');
ga.addParam('sysparm_name','addDurationToDateTime');
ga.addParam('sysparm_start_date', g_form.getValue('start_date'));
ga.addParam('sysparm_duration', g_form.getValue('planned_duration'));
ga.getXML(ResponseFunction);
function ResponseFunction(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
g_form.setValue('end_date',answer);
}
}
Server-Side Example:
var dtHelper = new global.addDurationToDateTime();
var new_end_date = addDurationToDateTime(current.start_date,current.u_planned_duration);
current.setValue('end_date',new_end_date);
*/
try {
// Parameters can be retrieved from both client and server-side...
var sd = this.getParameter('sysparm_start_date') || start_date;
var dur = this.getParameter('sysparm_duration') || duration;
var st = new GlideDateTime(sd); // Start Time
var dt = new GlideDuration(dur); // Duration
var du = dt.getNumericValue() / 1000;
st.addSeconds(du);
return st.getValue();
} catch (e) {
var msg = 'Script Include (NormalChangeTemplates > addDurationToDateTime): ' + e;
gs.error(msg);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2024 10:24 PM
To ensure that your script works regardless of the user's date time format, you need to handle date parsing and formatting explicitly.
addDurationToDateTime: function(start_date, duration) {
try {
// Parameters can be retrieved from both client and server-side...
var sd = this.getParameter('sysparm_start_date') || start_date;
var dur = this.getParameter('sysparm_duration') || duration;
// Parse start date using GlideDateTime, regardless of user's date format
var st = new GlideDateTime();
st.setDisplayValue(sd);
// Parse duration using GlideDuration
var dt = new GlideDuration(dur);
var du = dt.getNumericValue() / 1000;
// Add duration to start date
st.addSeconds(du);
// Convert end date to string in system format
var end_date = st.getDisplayValue();
return end_date;
} catch (e) {
var msg = 'Script Include (NormalChangeTemplates > addDurationToDateTime): ' + e;
gs.error(msg);
return ''; // Return empty string or handle error as needed
}
}
With these modifications, the function should correctly parse the start date and duration values regardless of the user's date format. It then adds the duration to the start date and returns the end date in the system's date format.
Make sure to update your client-side script to handle the response accordingly.
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2024 01:48 PM
Hello Jeff,
Please try to convert the start date into duration using the duration object and add them.
Answer my reply as correct if it was helpful Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2024 10:24 PM
To ensure that your script works regardless of the user's date time format, you need to handle date parsing and formatting explicitly.
addDurationToDateTime: function(start_date, duration) {
try {
// Parameters can be retrieved from both client and server-side...
var sd = this.getParameter('sysparm_start_date') || start_date;
var dur = this.getParameter('sysparm_duration') || duration;
// Parse start date using GlideDateTime, regardless of user's date format
var st = new GlideDateTime();
st.setDisplayValue(sd);
// Parse duration using GlideDuration
var dt = new GlideDuration(dur);
var du = dt.getNumericValue() / 1000;
// Add duration to start date
st.addSeconds(du);
// Convert end date to string in system format
var end_date = st.getDisplayValue();
return end_date;
} catch (e) {
var msg = 'Script Include (NormalChangeTemplates > addDurationToDateTime): ' + e;
gs.error(msg);
return ''; // Return empty string or handle error as needed
}
}
With these modifications, the function should correctly parse the start date and duration values regardless of the user's date format. It then adds the duration to the start date and returns the end date in the system's date format.
Make sure to update your client-side script to handle the response accordingly.
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 09:34 AM
This works, thank you!