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)>


<!— do dumps to show output —>

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

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

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


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

<cfif validDate>

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


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


<!— catch the unpredictable errors —>

<cfcatch type=“any”>

<cfset msg = “CFCATCH :” & CFCATCH>






Page 2 of 2 | Previous page

1 comment on this post.
  1. Francesco:


    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:

Leave a comment