Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need to Get the day based on entered date & time

is_12
Tera Contributor

Hi Community,

 

On the Catalog form I have field start & end date.

 

Based on the start date need to get the day

 And also based on the start date, need to get the next 3months & 6months & 12months date

 

Can anyone help me with this

 

Thanks,

 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@is_12 

try this and it will take care of any date/time format

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    // Parse string to milliseconds
    var dateMS = getDateFromFormat(newValue, g_user_date_time_format);
    var newDT = new Date();
    newDT.setTime(dateMS);

    // Example: Add 3 months
    newDT.setMonth(newDT.getMonth() + 3);

    // If you want 6 or 12 months, just change the +3:
    // newDT.setMonth(newDT.getMonth() + 6); // For 6 months
    // newDT.setMonth(newDT.getMonth() + 12); // For 12 months

    var val = formatDate(newDT, g_user_date_time_format);
    alert(val);
}

Output:I added 3 months, enhance further

AnkurBawiskar_1-1761291386161.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

@is_12 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

25 REPLIES 25

@is_12 

I think you can enhance the logic for this.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

 

@Ankur Bawiskar I Tried with this client script, 

    alert("hello");
    var date = g_form.getValue('start_date');
    var year = new GlideAjax('BT_CSR_SSR_CatalogUtils');
    year.addParam('sysparam_name', 'checkLeepyear');
    year.addParam('sysparam_date', g_form.getValue('start_date'));
    year.getXML(setData);

    function setData(response) {
        alert('hello 13'+ans);
        var ans = response.responseXML.documentElement.getAttribute('answer');
        var ansParsed = JSON.parse(ans);
        if (ansParsed == true) {
            alert('hello 16 if'+startDate);
            var startDate = new Date(date);
            startDate.setDate(startDate.getDate() + 366);
            g_form.setValue('day_1', ans);
        } else if (ansParsed == false) {
            alert('hello 21 else if'+startDate1);
            var startDate1 = new Date(date);
            startDate.setDate(startDate1.getDate() + 365);
            g_form.setValue('day_1', ans);
}
}
and script include :
    checkLeepyear: function() {
        gs.info("Year 59");

        var createdDate = new GlideDateTime(this.getParameter('sysparam_date'));

        gs.info("Year 63");
       
        var year = parseInt(createdDate.getYearLocalTime());

        var isLeapYear = (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);

        gs.info("Year: " + year + " is leap year? " + isLeapYear);
        return isLeapYear;


    },
 
Not getting any logs & getting answer alert has undefined
Seem's like function is not called
 
can you help me here

@is_12 

GlideAjax syntax is wrong

year.addParam('sysparm_name', 'checkLeepyear');
year.addParam('sysparm_date', g_form.getValue('start_date'));

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar ,

alert("hello");
    var start = g_form.getValue('start_date');
    var year = new GlideAjax('BT_CSR_SSR_CatalogUtils');
    year.addParam('sysparm_name', 'checkLeepyear');
    year.addParam('sysparm_date', start);
I have changes according, But still it is the same
answer alert, I'm getting has undefined and no log's

@Ankur Bawiskar start_date type is date time field

is it causing an issue ?