- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2022 08:31 AM
I am trying to calculate the end date using start date + duration but I am struggling as the duration is 01-01-1970 it is giving me the seconds since then and I can't add them to the Start date to get the end date. Any help would be much appreciated..
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2022 08:34 AM
Hi,
you can use this script to add duration to start date
something like this in server side
var gdt = new GlideDateTime(current.start_date.getDisplayValue());
var ms = current.u_dation.dateNumericValue();
gdt.add(ms);
current.end_date = gdt.getValue();
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2022 08:34 AM
Hi,
you can use this script to add duration to start date
something like this in server side
var gdt = new GlideDateTime(current.start_date.getDisplayValue());
var ms = current.u_dation.dateNumericValue();
gdt.add(ms);
current.end_date = gdt.getValue();
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2022 09:15 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2022 10:40 PM
Can you share complete script?
Ideally it should work fine
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2022 01:54 AM
Hi
https://community.servicenow.com/community?id=community_question&sys_id=7678f2efdb514110019ac2230596196a
1. In Knowledge Base the article validity is (ex:1825 = 5years / 2yrs) so article validity populate to Valid to date in Knowledge article to 5 yrs . i have written logic i can add more than 30 days . so(1825+30 days i can add) if i am try to add more than that it should print date in alert .user should select less than yyyy-mm-dd.
i tried printing months it is working as per below script i want print date instead of months
2. if article validity is empty by default valid to date is 2100-01-01 in these case it should print alert from current date to 1year +30= 395 days
below script is i tried for months can anyone help me for date
client script :
function onSubmit() {
//Type appropriate comment here, and begin script below
if (g_scratchpad.isFormValid){
return true;
}
var actionName = g_form.getActionName();
var ga = new GlideAjax('KnowledgeTimeDifference');
ga.addParam('sysparm_name','getDiff');
ga.addParam('sysparm_sysid',g_form.getValue('kb_knowledge_base'));
ga.addParam('sysparm_start',g_form.getValue('valid_to'));
ga.getXMLAnswer(function(answer) {
answer = JSON.parse(answer);
if(answer.status == false)
{
alert("Please select the Valid to date less than "+answer.days+" months greater than today's date");
}
else
{
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}
});
return false;
}
script include:
var KnowledgeTimeDifference = Class.create();
KnowledgeTimeDifference.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDiff: function() {
var arval = '';
//var stDateVar;
var sysid = this.getParameter('sysparm_sysid');
var gr = new GlideRecord('kb_knowledge_base');
gr.addQuery('sys_id', sysid);
gr.query();
if (gr.next()) {
arval = gr.getValue('article_validity');
}
if (arval == null) {
arval = '0';
}
var gdt1 = new GlideDateTime();
gdt1.addDaysUTC(parseInt(arval) + 30);
var days = 0;
if (arval == '0') {
days = 13;//yyyy-mm-dd
} else {
days = Math.round((parseInt(arval) + 30) / 30);
}
var start = this.getParameter('sysparm_start');
var gdt2 = new GlideDateTime(start);
var dur = GlideDateTime.subtract(gdt1, gdt2);
var date = new GlideDate();
date.setValue(gdt2);
date.addMonths(days);
var dif = dur.getNumericValue();
var js = {};
if (dif <= 0) {
js.status = true;
} else {
js.days = days;
js.status = false;
}
return JSON.stringify(js);
},
type: 'KnowledgeTimeDifference'
});