The CreatorCon Call for Content is officially open! Get started here.

Need to calculate 6 months by using new date method

Anupriya2
Tera Expert

I want to fetch Date after 6 months from todays date. I am using below code which is working fine but for the next year It is calculating 13(Jan2023), from july can someone help me with below code.

Please check dateplusone log it is giving 21-13-2022 instead of 21-01-2023

var now = new Date();
    alert('now -->'+now); //   now -->Thu Jul 21 2022 15:22:49 GMT+0530 (India Standard Time)
    var curr = now.getDate() + '-' + (now.getMonth()+1) + '-' + now.getFullYear();
    alert('curr -->'+curr); //  curr -->21-7-2022
    if (riskRating == 'Critical' || riskRating == 'High')
    {  
        //dateplusOne = now.getDate() + '-' + (now.getMonth()+7) + '-' + (now.getFullYear()); 
        alert('dateplusOne -->'+dateplusOne); //   dateplusOne -->21-13-2022
    }
   
    
    var current_date = moment(curr, 'DD-MM-YYYY');
    alert('current_date -->'+current_date); // current_date -->1658341800000
    //var current_plus_one = moment(dateplusOne, 'DD-MM-YYYY'); 
    var current_plus_one = dateplusOne; // current_plus_one -->NaN

1 ACCEPTED SOLUTION

Anupriya2
Tera Expert

It is solved by adding "dateplusOne = now.setMonth(now.getMonth() + 7);"

Thanks everyone:)

View solution in original post

7 REPLIES 7

Hi,

In the UI page client script also you can invoke GlideAjax

Regards
Ankur

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

suvro
Mega Sage

Here is your answer

https://community.servicenow.com/community?id=community_question&sys_id=38c40be9dbd8dbc01dcaf3231f9619e3

Anupriya2
Tera Expert

It is solved by adding "dateplusOne = now.setMonth(now.getMonth() + 7);"

Thanks everyone:)