Ask a Question

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

SOLVED
prabhjot88singh
Contributor

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

1 REPLY 1
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.

 

 


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
cancel
Showing results for 
Search instead for 
Did you mean: