– Common Function Library Project

TextToDate(textString[, format])

Last updated January 21, 2005


Bill Rawlinson

Version: 1 | Requires: CF6 | Library: DateLib

Takes a numeric string and returns the best date equivilent based on the format you specify. Accepts four different formats: mmddyyyy, yyyymmdd, ddmmyyyy, yyyyddmm. If a date string is passed in the date is returned. If a non-numeric or non-date string is passed in it returns 1/1/1900.

Return Values:
Returns a date.


<cfset datevals = "01599,4/5/1978,010599,1599,151999,01051999,010519999,010501999,159,10599,avkld,24587,343532,12423232,987822,311993">

            <th>Old Date</th>
    <cfloop list="#dateVals#" index="dateval">
            <td>#dateval# </td>


Name Description Required
textString Date as a string. Yes
format Format of the date. Valid values are: mmddyyyy,yyyymmdd,ddmmyyyy,yyyyddmm No

Full UDF Source:

 cffunction that takes a text string and a format and returns a date object.
 @param textString      Date as a string. (Required)
 @param format      Format of the date. Valid values are: mmddyyyy,yyyymmdd,ddmmyyyy,yyyyddmm (Optional)
 @return Returns a date. 
 @author Bill Rawlinson ( 
 @version 1, January 21, 2005 
<cffunction name="textToDate" returnType="string" output="false" hint="converts a numeric string to a date object">
    <cfargument name="textString" type="string"    requied="true" hint="numeric string to convert to a date object">
    <cfargument name="format" type="string" required="false" default="mmddyyyy"    hint="best guess at the format of the string; valid values are mmddyyyy | yyyymmdd | ddmmyyyy | yyyyddmm">

    <cfset var dateval    = arguments.textstring>
    <cfset var month    = "">
    <cfset var day        = "">
    <cfset var year        = "">
    <cfset var detelen = "">
    <cfset var maxDays = "">
    <!--- placeholders used to find month and date on strings between 4-6 characters long --->
    <cfset var sp        = "">
    <cfset var counter1    = "">
    <cfset var counter2    = "">

    <cfif isNumeric(dateval)>
        <cfset datelen = len(dateval)>

        <cfswitch expression="#datelen#">
            <cfcase value="1,2,3">
                <cfset day = 1>
                <cfset month = 1>
                <cfset year = "1900">
            <cfcase value="4,5,6">
                <cfif right(arguments.format,4) eq "yyyy">
                    <cfset year = right(dateval,2)>
                    <cfset dateVal = left(dateval,Len(dateval)-2)>
                    <cfset arguments.format = left(arguments.format,4)>
                    <CFSET year = Left(dateval,2)>
                    <CFSET dateVal = Right(dateval,Len(dateval)-2)>
                    <CFSET arguments.format = Right(arguments.format,4)>

                due to variable lenths of remaining numbers
                we have to figure out where to chop up the string to get 
                the month and day 
                <cfset dateLen = len(dateval)>
                <cfset counter1 = 2>
                <cfset counter2 = 2>
                <cfif dateLen EQ 3>
                    <cfset counter1 = 2>
                    <cfset counter2 = 1>
                <cfelseif dateLen EQ 2>
                    <cfset counter1 = 1>
                    <cfset counter2 = 1>

                <cfif left(arguments.format,2) EQ "mm">
                    <cfset month = mid(dateval,1,counter1)>
                    <cfset sp = 1 + counter1>
                    <cfset day = mid(dateval,sp,counter2)>
                    <cfset day = mid(dateval,1,counter1)>
                    <cfset sp = 1 + counter1>
                    <cfset month = mid(dateval,sp,counter2)>

            <cfdefaultcase><!--- datelen gt 6 --->
                <cfif right(arguments.format,4) EQ "yyyy">
                    <cfset year = right(dateval,4)>
                    <cfset dateVal = left(dateval,len(dateval)-4)>
                    <cfset arguments.format = left(arguments.format,4)>
                    <cfset year = left(dateval,4)>
                    <cfset dateVal = right(dateval,len(dateval)-4)>
                    <cfset arguments.format = right(arguments.format,4)>
                <cfif left(arguments.format,2) EQ "mm">
                    <cfset month = mid(dateval,1,2)>
                    <cfset day = mid(dateval,3,2)>
                    <cfset month = mid(dateval,3,2)>
                    <cfset day = mid(dateval,1,2)>

        <cfset year = mid(val(year),1,4)>
        <cfset year = 0 & year>

        <cfset month = month MOD 12>
        <cfif month EQ 0>
            <cfset month = 12>

        <cfset maxDays = daysInMonth(createDate(year,month,1))>
        <cfset day = day MOD maxDays>
        <cfif day EQ 0>
            <cfset day = maxDays>

    <cfelseif isDate(dateval)>
        <cfset day = day(dateval)>
        <cfset month= month(dateval)>
        <cfset year= year(dateval)>
         <!--- if an invalid string is passed in we return 1/1/1900 --->
        <cfset day = 1>
        <cfset month = 1>
        <cfset year = "1900">

    <cfreturn createDate(year,month,day)>



Latest Additions

Raymond Camden added
November 04, 2017

Leigh added
May 11, 2016

Raymond Camden added
May 10, 2016

Kevin Cotton added
May 05, 2016

Raymond Camden added
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson