Need help with change management start_date + duration = end_date

Jeff Schodde
ServiceNow Employee
ServiceNow Employee
I have a script include that works fine unless the user changes the date format on their profile to anything besides the system format. So, I need this script to work regardless of the user's date time format.
 
The change_request dictionary has these three fields:
  • start_date (glide_date_time)
  • u_planned_duration (glide_duration)
  • end_date (glide_date_time)
I'm trying to get this to work from a client script (onChange of start_date) that is NOT in the Global scope. I can put it into Global scope if needed.
 
Here's the script include function, and it's in the Global scope:
 
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);
        }
1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@Jeff Schodde 

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

View solution in original post

3 REPLIES 3

chaitu1
Tera Contributor

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

Maddysunil
Kilo Sage

@Jeff Schodde 

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

Jeff Schodde
ServiceNow Employee
ServiceNow Employee

This works, thank you!