Creating ColdFusion ()

I just learned about the ColdFusion feature CreateDate

. But, when I use CreateDate, the value output is different. I mean, it changes every day and every day.

<cfoutput>
<cfset txtBirthDate='07-10-1983'>
<cfset valueOf_txtBirthDate =  dateFormat(CreateDate(Year(txtBirthDate),Month(txtBirthDate),Day(txtBirthDate)),'YYYY-MMM-DD')>

#txtBirthDate#<br/><br/>
#valueOf_txtBirthDate#<br/>
</cfoutput>

      

The txtBirthDate value is 07-10-1983 , but the valueOf_txtBirthDate value , which was CreateDate

created by 1983 -Jul-10 . Why is it July? It is supposed to be October: 07 (date), 10 (month), 1983 (year).

Is there something wrong with the format?

+3


source to share


6 answers


You are using in a createDate()

very confusing way. txtBirthDate

is not a date, so you shouldn't use it as input to date functions like year()

, month()

etc. At the time you use the date function, the input must already be a date object.

Let's say you start at a line 07-10-1983

where the format is mm-dd-yyyy

.

txtBirthDate ='07-10-1983'; // dd-mm-yyyy

// extract the specific date parts from the string
yyyy = listLast(txtBirthDate, "-");
mm = listGetAt(txtBirthDate, 2, "-"); 
dd = listFirst(txtBirthDate, "-");

// create a date object out of those components
birthDate = createDate(yyyy, mm, dd);

// output the date object in human readable format
writeOutput(dateFormat(birthDate, "YYYY-MM-DD"));

      



(Obviously, in real code, you wouldn't have comments like these that only serve to indicate the obvious!)

Use only dateFormat()

at the last point in withdrawal. For other operations with the date using the date of the actual object: birthDate

.

+7


source


Because by default your date is in mm / dd / yyyy format. So

<cfset txtBirthDate='07-10-1983'>

      



will read 10 July 1983 CF. Hence it follows ...

+1


source


You can use a Java function to accomplish this. This is how it works:

<cfset txtBirthDate='07-10-1983' />
<cfset formatter = createObject("java","java.text.SimpleDateFormat") />
<cfset formatter.init("dd-MM-yyyy") />
<cfset birthDate= formatter.parse(txtBirthDate) />

      

0


source


It seems like this should do the trick:

 <cfset txtBirthDate = '10-7-1983'>
 #DateFormat(txtBirthDate, "MMM, D, YYYY")#

      

Note that I changed the position of the month and day in the cfset to make it correct.

0


source


you can do something simple:

txtBirthDate = '07-10-1983'; //dd-mm-yyyy
arrayDate = listToArray(txtBirthDate, '-');
date_birthDate = createDate(arrayDate[3], arrayDate[2], arrayDate[1]);

writeOutput(lsDateFormat(date_birthDate, 'yyyy-mm-dd');

      

0


source


<cfscript>
    txtBirthDate=CreateODBCDate("1983-10-07");
    txtBirthDate=LSDateFormat(txtBirthDate, "dd/mm/yyyy");
</cfscript>

<cfoutput>
    #txtBirthDate#
</cfoutput>

      

0


source







All Articles