Using Coldfusion to turn a string into a date

<cfset filePath = ExpandPath(“Example.txt”)>

<!— read the file in as a variable —>

<cffile action=“read” file=“#filePath#” variable=“myFile”>

<!— create the delimiter —>

<cfset line=chr(13)&chr(10)>

<!— get the first line of the txt file —>

<cfset date = listGetAt(myFile,1,line)>

<!— get rid of the left and right characters… which are quotes(“) —>

<cfset date = replace(date,'”‘,“”,“All”)>

<!— convert the string passed, into a date —>

<cfset date = DateFormat(date,“mm/dd/yyyy”)>

<!— set boolean to test off of, returns a true/false —>

<cfset validDate = isDate(date)>

<cfif isDefined(“URL.DEBUGMODE”) AND URL.DEBUGMODE NEQ False>

<!— do dumps to show output —>

Date:<cfdump var=“#date#”>

Date with no quotes:<cfdump var=“#date#”><br />

“isdate?”<cfdump var=“#validDate#”><br />

</cfif>

<!— serverside checking to validate it’s a good date, if not show an error —>

<cfif validDate>

<cfset msg = “This is a valid date : “ & date>

<cfelse>

<cfset msg = “the date was invalid in your file, please correct, and retry”>

</cfif>

<!— catch the unpredictable errors —>

<cfcatch type=“any”>

<cfset msg = “CFCATCH :” & CFCATCH>

</cfcatch>

</cftry>

<cfoutput>

#msg#

</cfoutput>

Page 2 of 2 | Previous page

1 comment on this post.
  1. Francesco:

    Hi,

    I need to convert a string like this January, 19 2005 07:35:42 because i must insert in a database(MySql).
    Could you help me?

    This come out when try to update a cfgridcolumn datefield… as on the link above is explained:
    http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7a01.html#WSe9cbe5cf462523a0-6ada7e31123088f2cf8-7fff

Leave a comment