• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1022
  • Last Modified:

VFP Simple Date Calculation

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))

Open in new window

0
formadmirer
Asked:
formadmirer
  • 6
  • 2
1 Solution
 
formadmirerAuthor Commented:
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))

Open in new window


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
 
formadmirerAuthor Commented:
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
 
formadmirerAuthor Commented:
SET DATE TO AMERICAN
ldToday = DATE()
ldRegisterDate = "09/05/2012"
ldRegisterDate = CTOD(ldRegisterDate)
lnDaysRemaining = (ldRegisterDate + 90) - ldToday

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

Open in new window

This works. Is this the best method?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
formadmirerAuthor Commented:
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
 
pcelbaCommented:
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))

Open in new window


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
 
formadmirerAuthor Commented:
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
 
pcelbaCommented:
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
 
formadmirerAuthor Commented:
Thank you!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now