Skip to main content
Known Participant
October 4, 2010
Answered

formatting a date variable

  • October 4, 2010
  • 2 replies
  • 1715 views

I want to format the value of date which is in a variable(date_var) in MM-DD-YYYY format and print it in a document.

                     <td>
                         #DateFormat(#date_var#, 'MMMM DD,YYYY')# does not work
                     </td>

does not work.

How can I do that?

This topic has been closed for replies.
Correct answer

Plz try this...

<cfset

date_var = now()>

<cfset t =DateFormat(date_var, 'MMMM DD,YYYY')>

<cfdump

var="#t#">

<cfdocument

filename="d:\testdate.pdf" overwrite="yes" format="PDF">

<cfoutput>#t#</cfoutput>

</cfdocument>

2 replies

Inspiring
October 4, 2010

If you are wanting to format a date as MM-DD-YYYY, why are you passing dateFormat() a mask of MMMM DD,YYYY?

And what do you mean by it "does not work"? What - other than what I mentioned - does it do that you don't want?

--

Adam

Inspiring
October 4, 2010

If you are wanting to format a date as MM-DD-YYYY, why are

you passing dateFormat() a mask of MMMM DD,YYYY?

My take on it was they have a variable containing a date string in format MM-DD-YYYY. They wish to reformat it as MMMM DD,YYYY.

But I could be wrong ...

Inspiring
October 4, 2010

Oh I see. Yes, that's probably it. I guess, in that case, the intermediary step needed is to separate out the yyyy, mm & dd bits from the original string - using list functions or left/right/mid or whatever - then createDate() with that lot, •the• call dateFormat(). Don't pass a string to a date function is the advice here.

Of course the "not working" could still be something else. I would have expected CF to be able to cast a string formatted as mm-dd-yyyy to a date object implicitly. Maybe not. It's always preferable to do these things "properly" though, I reckon.

--

Adam

Correct answer
October 4, 2010

Plz try this...

<cfset

date_var = now()>

<cfset t =DateFormat(date_var, 'MMMM DD,YYYY')>

<cfdump

var="#t#">

<cfdocument

filename="d:\testdate.pdf" overwrite="yes" format="PDF">

<cfoutput>#t#</cfoutput>

</cfdocument>

h_c1Author
Known Participant
October 4, 2010
Thanks cfinnov,

I initially wanted it so that if date_var had value Jan 5, 2010, it was shown as 01-05-2010

But, after that was done the users wanted the month to be  spelled out so that if date_var had value Jan 5, 2010, they wanted  January 5, 2010.

I googled for the Live docs and changed the mask as  #DateFormat(date_var, "MMMM-DD-YYYY")# which did it. You also suggested the same.

Thanks for your advice and time. I have marked your answer as correct and helpful for the other responders.