cancel
Showing results for 
Search instead for 
Did you mean: 

How to use variable in DateAdd function

Former Member
0 Kudos

Hi,

In CBTA

I initialized a variable(V_DATE) with current date using expression-

%=day(now)&"."&month(now)&"."&year(now)%

Then I want to add 1 to day,so I used the below expression-

%=DateAdd("d",1,$V_DATE$)%

But the DateAdd function is not working.

Can anyone tell me how to use variable(V_DATE) in DateAdd function.

Thanks in advance for your help

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Archana,

If your requirement is to have next day date, you can create one more variable as "V_NEXT_DATE' with expression

%=(day(now)+1)&"."&month(now)&"."&year(now)%


This will give tomorrows date always. Same you can do for next month if required.


This is easiest way but may fail if current date is 30/31st. If you want to make is more intelligent you need to write custom function.


Hope this helps you.


Regards,

Sachin

TomCenens
Active Contributor
0 Kudos

Hi

Check out this blog post:

Kind regards

Tom

Former Member
0 Kudos

Hi Tom,

I already checked your blog. But if I use &Date then I have to change my system time format.

In order to make code flexible enough to run in any system without any pre-settings, I should use variables which will change dynamically.

Moreover,when I am using the same function for time its working fine as shown below but not working for date

%=DateAdd("n",-30,$V_TIME_STRT$)%

where V_TIME_STRT=%=Right("00"&Hour(Time),2)&":"&Right("00"&Minute(Time),2)&":"&Right("00"&Second(Time),2)%

Thanks & Regards,

Archana

TomCenens
Active Contributor
0 Kudos

Hi Archana

You're not supposed to use &date?

The blog goes on and the code in the end is the snippet we use:

%=day(DateAdd("m",1,now()))&"."&month(DateAdd("m",1,now()))&"."&year(DateAdd("m",1,now()))%


This is visual basic script code. Have you tried this?

Kind regards

Tom

Former Member
0 Kudos

Hi Tom

The provided code will work fine if we know the number of days/month/year to add/substract.

For e.g.- If we want last date of current month then

1. Store 1st day of next month in var

      V_NEXT-->%="1."&month(DateAdd("m",1,now()))&"."&year(now)%

2. Substract 1 from V_NEXT to get last date of current month
      %=DateAdd("d",-1,$V_NEXT$)%

But here the DateAdd() is not working

Do you have any alternative solution to this?

Thanks & Regards,

Archana

TomCenens
Active Contributor
0 Kudos

Hi Archana

Mm, haven't tried that before so I'm not sure that will work like that to use a variable in inline VB script. I would say write a custom function by using the custom library and pass on the variable as input and get the data back as output.

Again, I haven't tried it but that's what I would expect what should work out.

Best regards

Tom