String to date conversion

Discussion in 'Programming' started by youjie, Nov 15, 2006.

  1. #1
    Hi anyone knows how to convert a string to a date variable in coldfusion?

    i have a string "200611" which is in yyyymm format andi wish to convert it into a date variable of yyyymm format

    thanks in advance ...
     
    youjie, Nov 15, 2006 IP
  2. seamus.hogan

    seamus.hogan Peon

    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi youjie,

    you could try this.

    <!--- set the date string --->
    <cfset input = "200611">

    <!--- extract the first 4 characters from the string --->
    <cfset yearpart = mid(input, 1,4)>

    <!--- extract the last 2 characters from the string --->
    <cfset monthpart = mid(input, 5,2)>

    <!--- create a date using the two parts --->
    <cfset date = createdate(yearpart, monthpart, 01)>

    <!--- output the date with the formatting --->
    <cfoutput>#dateformat(date, 'yyyy/mm')#</cfoutput>

    Hope this helps
     
    seamus.hogan, Nov 15, 2006 IP