Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Calculations 2

Status
Not open for further replies.

Miss Su

IS-IT--Management
Feb 8, 2019
2
PH
Can somebody teach me on how to calculate a date in visual fox pro 9.0. For example. When i enter a date of PO "10-10-18 " In another text box , automatically compute the due date which is 11-10-18. How can i do that? Thank you for your response. I am newbie in visual vfp.
 
Assuming that your dates are stored with the Date datatype (as opposed to a Character data type that contains digits that happen to represent a date), then you can simply add or subtract a number from one date to get another date. So in this case, you would simply add 1 to the order date to get the due date.

If the dates are stored in textboxes, you use the Value property of the textbox to set or get the date, so you could do something like the following:

THISFORM.Text2.Value = THISFORM.Text1.Value + 1

If the textboxes contain Character data, then it is more difficult, as you would have to convert the characters to a valid date, and that involves knowing the date format (MM/DD//YY, DD-MM-YYYY, etc) and how to handle two-digit years.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I interpret your question as asking how to convert a text date to a date value.

Well, there's CTOD() for that, but it's easiest if you bind a textbox to a field or property being a date or initialize the value (simply set the textbox.value in the designer in the property sheet) to a date.
For example right click on the textbox for date entry, select "Properties..." from the context menu and then in the Data Tab in the Value property enter =DATE() and the textbox is initialised to the current date.

Once a textbox displays and has a date value, you can only enter valid dates and textbox.value will be a date, no need to convert it.

CTOD could be relevant to transform data you get as text, it depends on settings of SET DATE, SET MARK TO (caution, there are other SET MARK commands about menu items) and SET CENTURY, there is no automatism to take any text that could resemble a date and convert it and since some formats are month first, others day first, there are ambiguous dates, so you have to first set the way you want dates to display and then you can also reverse texts in that format to dates.

The preferred way to specify dates in code, for example, that is independent of settings is either call DATE() or DATETIME(). Without parameters they provide the values of today and now, parameters allow you to specify a specific date, eg DATE(1999,12,31) but besides needing to call a function date literals are possible in the form {^yyyy-mm-dd}, so you can code datevar = {^1999-12-31}, this also works independant on what the current MARK setting is, even if you have other countries marks like / or . you can always use the dash here and just like DATE() the date literals only allow ymd order. Caution and notice: The string "{^1999-12-31}" doesn't automatically convert into a date, you really write curly brackets as delimiters in code and when you enter such a literal into a textbox that does not come out as such a date but such a string, you can EVAL("{^1999-12-31}"), though, to get that as the date it means.

Besides that, you can compute with dates, like DATE()-7: Date a week ago, DATEIME()+60: DateTime in a minute. GOMONTH(DATE(),1): Date in a month (look into the help topic about how 31 turns into 30 of next month, for example and other special cases).

Bye, Olaf.

Olaf Doschke Software Engineering
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top