Microsoft KB Archive/131254

= How to Manipulate DateTime Fields in a Grid =

Article ID: 131254

Article Last Modified on 8/24/1999

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition

-



This article was previously published under Q131254



SUMMARY
Microsoft Visual FoxPro introduces a new data type (DateTime) that allows a date and time value to be stored in a single field. By default, Visual FoxPro displays the data stored in a DateTime field in a "MM/DD/YY HH:MM:SS XM" format. This article shows by example how to display just the date or just the time portion of a DateTime field in a Visual FoxPro Grid.



MORE INFORMATION
Visual FoxPro does not offer a command or function to automatically retrieve just the Date or just the Time portion of a DateTime field. The code provided below creates two new methods (GetTime and GetDate) that can be called by the Grid Column ControlSource property to display just the date or just the time of a DateTime field.

Step-by-Step Example
The following example assumes that a table already exists with a DateTime field named tDateTime.

  Create a new form by using this command: CREATE FORM newform  Add the proper table to the Data Environment by choosing Data Environment from the Form menu. Add a Grid to the form by dragging the Grid control from the Form Controls toolbar to the form. Change the ColumnCount property of the Grid to 2 (any number greater than zero will work). From the Form menu, choose New Method. Name the new method GetTime. Create a second new method named GetDate. From the Properties sheet, select the Form1 object, and then select the Methods tab. Open the GetTime method.</li>  Place the following code in the GetTime method: PARAMETERS tField justtime = SUBSTR(TTOC(tField),10) RETURN justtime The tDateTime field will be passed to the GetTime method from the Grid Column. The TTOC function converts the field to character so that SUBSTR can retrieve just the time portion of the field and return it to the grid. </li>  Place the following code in the GetDate method: PARAMETERS tField justdate = SUBSTR(TTOC(tField),1,8) RETURN justdate The tDateTime field will be passed to the GetDate method from the Grid Column. The TTOC function converts the field to character so that SUBSTR can retrieve just the date portion of the field and return it to the grid. </li> Using the Properties sheet, select the Header1 object, and change the caption property to Just Time.</li> Select the Header2 object, and change the caption property to Just Date.</li>  Change the ControlSource property of Column1 to: thisform.gettime(tDateTime) </li>  Change the ControlSource property of Column2 to: thisform.getdate(tDateTime) </li> Save and run the form. The time portion of the tDateTime field displays in Column1 and the date portion displays in Column2. Note that the fields are read-only.</li></ol>

Additional query words: VFoxWin

Keywords: KB131254

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.