adding time to dates using now()

333 views
Skip to first unread message

JasonSpr

unread,
Nov 26, 2009, 1:25:33 PM11/26/09
to XMPie Interest Group
I'm trying to put an expiry date on some coupons, the expiry date
being 90 days after the day of printing. So far I've been doing this
by parsing the now() function using substring(), evaluating it asnumber
(), incrementing, and then evaluating back as date. This stops working
on days like November 30th - I get an error when it tries to evaluate
Febuary 30th as a date.

Is there a way to increase now() by 90 days without parsing everying?

couch

unread,
Nov 26, 2009, 10:52:32 PM11/26/09
to XMPie Interest Group
Are you using uPlan? If so, it is a simple matter to create a function
which replicates the VBscript DateAdd()

JasonSpr

unread,
Nov 27, 2009, 11:26:59 AM11/27/09
to XMPie Interest Group
Yes, using uPlan 4.5.2. Can you provide more information on how to do
that?

couch

unread,
Nov 28, 2009, 12:40:27 AM11/28/09
to XMPie Interest Group
In the Plan Objects area, right click on functions and select:
New Function -> VBScript -> Date

Name the new function AddDate
Right click on the new AddDate function and Add the following three
parameters:
startDate (type=Date)
num (type=number)
datePart (type=string)

Now double click on the AddDate function, in in the top area, under
where it says Function AddDate(startDate, num, datePart) add the
following code:

<cut below this line>
' This function is to replicate the VB dateAdd() inside of uPlan.
'
' requires three attributes: startMonth, num, datePart - similar to
the VB function.
' startDate is the date to apply the calculation to, num is the
integer to add, valid entries for datePart are:
' yyyy - Year
' q - Quarter
' m - Month
' y - Day of year
' d - Day
' w - Weekday
' ww - Week of year
' h - Hour
' n - Minute
' s - Second

startDate = startDate
num = num
datePart = datePart
AddDate = dateAdd(datePart, num, startDate)

<end cut above this line>

You can see the actual function is just a few lines. The lines
starting with ' are comments to show you what you can enter for the
datePart...

Now how you use it:

In the ADOR object or variable where you want to do the date math;
simply drag up to the expression window your new function, and replace
the parameter names with the required values.

EG:

AddDate(|->[DatabaseDate],90,"d") <- will add 90 days to the
DatabaseDate
AddDate(|->[DatabaseDate],-1,"m") <- will remove one month from the
DatabaseDate

Of course, you can also use it with XMPie's date formatting functions,
eg:

FormatDate(AddDate(|->[DatabaseDate],90,"d"),"dddd, MMMM dd, yyyy")

Timothy Perrett

unread,
Nov 28, 2009, 8:43:14 AM11/28/09
to xmpie...@googlegroups.com
Steve,

Have you considered factoring this out to a uPlan extension? Seems like it would be a pretty useful utility :-) One would assume that these functions existing in the cscript implementation of VB would also be implemented in C# / VB.NET... either way, it would be fairly straight forward to bridge the managed / unmanaged code using the extension bridge to make this code portable.

Cheers, Tim
> --
>
> You received this message because you are subscribed to the Google Groups "XMPie Interest Group" group.
> To post to this group, send email to xmpie...@googlegroups.com.
> To unsubscribe from this group, send email to xmpie-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/xmpie-users?hl=en.
>
>
>

couch

unread,
Nov 28, 2009, 11:06:38 PM11/28/09
to XMPie Interest Group
Hi Tim,
Personally, I find it quick and easy to use VBscript of JavaScript
functions in the plan itself.
You're right, it could be done in qLingo extension, but I constantly
have problems with this.
(The problem is entirely mine though - Even Galit's sample VS solution
fails for me, but works when ever I go to show someone in Israel, then
fails again the next time I try to use it.)

Timothy Perrett

unread,
Nov 29, 2009, 7:07:59 AM11/29/09
to XMPie Interest Group
Hey Steve,

The thing that bugs me about uPlan right now is not being able to have
"libraries" as it were that already include default functions (such as
this) which one might use pretty regularly. To that end, making uPlan
extensions is the only real way to make code portable.

As you detail, the bridge is not perfect - the only real robust way to
build these extensions is in C++ (sigh)

Cheers, Tim

JasonSpr

unread,
Nov 30, 2009, 12:28:48 PM11/30/09
to XMPie Interest Group
Thank you for the informative replies. Steve, +5 for the most complete
answer I've ever recieved for anything on the internet. I had no idea
that uPlan had that sort of capability, building new functions based
on VBscript or JavaScript. Very good to know about this.

Cheers,
JG
> > fails again the next time I try to use it.)- Hide quoted text -
>
> - Show quoted text -

alexcar45

unread,
Aug 26, 2014, 11:31:16 PM8/26/14
to xmpie...@googlegroups.com
This script works like a charm, I tried it to add 7 days to a Now() write back from a PURL in uProduce 6.6.3, perfection!

|->[ADOR] := FormatDate(AddDate(Now(),7,"d"), "MM-dd-yyyy HH:mm")
Reply all
Reply to author
Forward
0 new messages