Solved

# VFP Simple Date Calculation

Posted on 2012-09-16
890 Views
I seem to have totaly forgotten how to calculate dates in fp.
I am just trying to get the remaining days of a 90 day trial from when the customer registered, but am drawing a blank.

``````ldToday = DATE()
ldRegisterDate = CTOD(09/05/2012)
lnDaysRemaining = ldRegisterDate + 90 - ldToday

WAIT WINDOW "remaining days = " + ALLTRIM(STR(lnDaysRemaining))
``````
0

Author Comment

Well, I'm getting close because I just tried

``````SET DATE TO AMERICAN
ldToday = DATE()
ldRegisterDate = {^2012/05/09}
lnDaysRemaining = (ldRegisterDate + 90) - ldToday

WAIT WINDOW "remaining days = " + ALLTRIM(STR(lnDaysRemaining))
``````

and I get -40, which isn't correct, but at least it's not an operator error.
The curly brackets are used because I read a post on a forum that suggested them. however, years ago I had written a complex file backup system that calculated all kinds of dates and I know I didn't use curlys then. Also, I'm unsure what the carat (I think it is) does, the ^, because I've never used it before either.
0

Author Comment

Ok, I swapped the month and date and the calculation is correct.
However, the date the I will be using will be in a different format, it looks like 09/05/2012, not 2012/09/05

So I still need either an answer for my first post, or a way to convert this date format that will work with the calculation in my last post.
0

Author Comment

``````SET DATE TO AMERICAN
ldToday = DATE()
ldRegisterDate = "09/05/2012"
ldRegisterDate = CTOD(ldRegisterDate)
lnDaysRemaining = (ldRegisterDate + 90) - ldToday

WAIT WINDOW "remaining days = " + ALLTRIM(STR(lnDaysRemaining))
``````
This works. Is this the best method?
0

Author Comment

I tried to delete this question but it won't let me, I guess because it now has comments, which are mine.

So go ahead and shoot me whatever tips you might have and I'll close it out then - thanks!
0

LVL 41

Expert Comment

As you see CTOD() isn't good because it depends on current SET DATE setting (and this can differ between compilation and run-time). You should use regionally independent date format as you attempted with {^yyyy.mm.dd}.

So I would just slightly update your last code:
``````ldToday = DATE()
ldRegisterDate = {^2012/09/05}    && always YYYY-MM-DD  format
lnDaysRemaining = (ldRegisterDate + 90) - ldToday

WAIT WINDOW "remaining days = " + ALLTRIM(STR(lnDaysRemaining))
``````

Another possibility is to use DATE() function to create date constant:
ldRegisterDate = DATE(2012, 9, 5)  && parameters mean again year, month, day

All above date constants are independent on SET DATE setting, so the code is easily portable.

The ALLTRIM(STR(...)) combination can be replaced by TRANSFORM() but it does not mean any advantage in this case.
0

Author Comment

Really? Because I was going to go with the CTOD because it worked with the date formatted the way it will be provided to me.

I am pulling the date from the registry and it is in the format of mm/dd/yyyy so how then would I use that in:
ldRegisterDate = DATE(2012, 9, 5)  && parameters mean again year, month, day

Thanks!
0

LVL 41

Accepted Solution

You did not mention registry before so I've been describing date constants usage in FoxPro. Windows Registry allows to store the date value as a string or integer and it is just on you who decides about the format so it can be mm/dd/yy or Julian day etc. and you have to process it based on the known format.

And remember users are eager for knowledge so somehow encrypted Julian day number is not bad option probably.

To answer your question: If the registry contains mm/dd/yyyy string already then CTOD() is good option. You just have to ensure SET DATE AMERICAN and SET STRICTDATE TO 0 or 1 in your app.
0

Author Closing Comment

Thank you!
0

## Featured Post

### Suggested Solutions

Microsoft Visual FoxPro (short VFP) is a programming language with it’s own IDE and database, ranking somewhat between Access and VB.NET + SQL Server (Express). Product Description: http://msdn.microsoft.com/en-us/vfoxpro/default.aspx (http://msd…
If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…