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

Mohith Devatte
Tera Sage
Tera Sage

Hello @Anupriya ,

Please try this script 

    if (riskRating == 'Critical' || riskRating == 'High')
    {  

    
var gdDate = new GlideDate(),  


var gdFormatted;  


gdFormatted = gdDate.getByFormat("dd-MM-yyyy");  


var gdtDate = new GlideDateTime(gdFormatted);  


gs.info(gdtDate.addMonths(6));
} 

Please mark my answer correct if it helps you

This is not working (I am writing this on UI page Client script)

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you want to add 6 months to today right?

then why not use glideAjax and use GlideDateTime methods

Regards
Ankur

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

This is not working (I am writing this on UI page Client script)