English

How To: Work with dates in a user-specified format

Summary

Instructions provided describe how to work with date values, in a string field, in a format specified. By default, dates in Arcview are represented by an 8-digit number in the format of YYYYMMDD.

Procedure

  1. Open a table that contains a date field.
  2. Select Start Editing from the Table menu.
  3. Add a new string field called Date_String.

    A. Open the table and Start Editing, if necessary.
    B. Select Add Field from the Edit menu.
    C. In the Field Definition dialog box, define the following:
    a) Set Name to an appropriate name, for example 'Name'.
    b) Set Type to String.
    c) Set Width to an appropriate number, for example '32'.
    D. Click OK.
    E. Select Stop Editing from the Table menu and save the edits.

  4. Make the Date_String field active and click the Calculator button.
    [O-Image] Field Calculator Button
  5. Enter the following in the Expression text box:

    Code:
    [Date].SetFormat( "M, d, yyyy").AsString

    [O-Image] Date.SetFormat Illustration
    Where [Date] is the field that contains the date value; .SetFormat changes the format of the date value; and .AsString creates a string object from a a date value.

  6. Define the date format of choice inside the parenthesis of the 'SetFormat' request. For example:

    Code:
    [Date].SetFormat("M, d, yyyy").AsString


    Code:
    [Date].SetFormat("yyyy").AsString


    Code:
    [Date].SetFormat("MMM dd, yyy").AsString


    Note:
    For more information on the various date formats, see the help topic 'Dates.'

  7. Click OK. Dates should be in the format specified.


    [O-Image] Date to String
    Note:
    If there is a single field, and a different format is desired,
    use the SetFormat request on that field.


    Note:
    To perform a query on dates contained in the String field, set the default date format to the format used for the date String field.

    1. Open a new Script Window.
    2. Copy the following line of code to the window, which may vary depending on the date format specified.

    Code:
    Date.SetDefFormat("M/d/yyyy").AsString

    3. Click the Compile button.
    [O-Image] Script compile button

    4. Click the Run button.
    [O-Image] Run compiled script button

    Then use one of these syntax samples to query the data information:

    Code:
    ( [Date_String].AsDate >= "12, 03, 1996".AsDate)


    Code:
    ( [Date_String].AsDate > "1972".AsDate)


    Note:
    The AsDate request will create a date object from the string value.