Forum Discussion

prabhjot88singh's avatar
prabhjot88singh
Contributor
5 years ago
Solved

How to Convert String to Date, Add some Days to it and convert back to String

Hello Users,

 

I have one query here and stuck in one of the places:

 

I have DATE column with value as 10-JAN-2020,(i.e. Date-Month-Year) format.I have already stored this value in a variable

 

I need to convert this STRING to Date FORMAT ,

ADD 5 Calendar Days to it and

convert back this Date to String and Print the NEW OBTAINED DATE.

 

i.E. as per screenshot, below are the steps needed :

Step 1 : Already completed from my side. FETCHED PAYPERIOD ENDDATE Column Value .i.e. 10-JAN-2020, and stored it in a variable name as X.

Step 2 : Convert this String variable obtained in Step 1 to DATE FORMAT format.

Step 3 : Add 5 CALENDAR days to this date, i.e. (10-JAN-2020)+ 5 DAYS = 15-JAN-2020

Print this value : 15-JAN-2020 in the console bar.

 

Can someone please share the logic for this code.

 

Best,

Prabhjot

  • Your going to use a combination of aqConvert, aqDateTime, and aqString methods to do what you want.  Here's what I did in JavaScript

     

    function convertString(dateString, daysToAdd){
        var varDateTime;
        varDateTime = aqConvert.StrToDateTime(dateString);
        varDateTime = aqDateTime.AddDays(varDateTime, daysToAdd);
        return aqString.ToUpper(aqConvert.DateTimeToFormatStr(varDateTime, '%d-%b-%Y'));
    }
    
    function test(){
        Log.Message(convertString('31-JUL-2019', 5))
    }

    run the Test function and I get 05-AUG-2019 in my log.

     

     

1 Reply

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Your going to use a combination of aqConvert, aqDateTime, and aqString methods to do what you want.  Here's what I did in JavaScript

     

    function convertString(dateString, daysToAdd){
        var varDateTime;
        varDateTime = aqConvert.StrToDateTime(dateString);
        varDateTime = aqDateTime.AddDays(varDateTime, daysToAdd);
        return aqString.ToUpper(aqConvert.DateTimeToFormatStr(varDateTime, '%d-%b-%Y'));
    }
    
    function test(){
        Log.Message(convertString('31-JUL-2019', 5))
    }

    run the Test function and I get 05-AUG-2019 in my log.