Formatting date/time values

I am looking at the "Aggregate List Items Across Sites" example and want to format the "Created" field so it appears as MM/dd/yyyy hh:mm [AM|PM] (or the standard "General" format in .NET).  When I use the following statement:

$dataRow.get_item("Created").ToString("g")

or,

$dataRow.get_item("Created").ToString("MM/dd/yyyy hh:mm tt")

the exact string is output with the page instead of being formatted.  I pulled the ToString() approach from the SQL Query example.  What is the correct way to accomplish this?

Have more questions? Submit a request

1 Comments

  • 0
    Avatar
    Matthew McBride

    Turns out I answered my own question.  The "Created" field (and other date/time fields) are actually returned as strings and not date/times.  To make this work, use the $DateTimes.Parse(...) method to convert the string into an actual date/time value then use the format string to render as desired.

    For example, use the following statement to get the date/time value:

        #set( $createdDate = $DateTimes.Parse($dataRow.get_item("Created")) )

    then, render using:

        $createdDate.ToString("g")

    This works perfectly.

Please sign in to leave a comment.