DateFormat syntax

Happy New Year!

I've successfully created my first CF page, but the dates are displayed as:
1986-11-15 00:00:00
when I want them to be displayed as:
15th November 1986

I can't get the syntax of the DateFormat function correct. The examples in Ben Forta's book only use the Now comand to show the current date and time, when I want to use the Date field from my database. What's the correct syntax combining DateFormat and my field 'Date'. And, does this DateFormat function go in the CFQUERY tag, or the CFOUTPUT tag?

cheers
drstueyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HelicopterCommented:
In a cfoutput tag you would use:

<cfoutput>
#DateFormat(DATE,"dd/mmmm/yyyy")#

</cfoutput>


The structure is DateFormat(date,[,mask])

The optional mask parameter is a string made up of d,m and y. There is a good example in the language reference that comes with the software.

Personally I wouldn't attempt the conversion inside the query, especially as yours seems to be working correctly.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OeilNoirCommented:
i see 2 ways to do what you want, there is one problem with DateFormat, it's that i couldn't find a way to make apear the St, Nd, Rd and Th, that's why i suggest 2 ways to do it. if you don't mind giving up the "st" then simply use the DateFormat function as you can see is below.

Now if you want to use the "st" the best way i found was to build the date yourself, with Day, Month, MonthAsString and Year function from the date you are willing to display.

Here is the exemple

<CFOUTPUT>

Date with DateFormat:
#DateFormat(now(),"dd mmmm yyyy")#
<BR>
<BR>

<!--- If you want to keep the "st" build the date yourself --->

Date Built Manualy :
<CFIF #day(now())# IS "1" or #day(now())# is "11" or #day(now())# is "21" or #day(now())# is "31">
      #day(now())#st #MonthAsString(Month(Now()))# #Year(Now())#
<CFELSEIF #day(now())# is "2" or #day(now())# is "12" or #day(now())# is "22">
      #day(now())#nd #MonthAsString(Month(Now()))# #Year(Now())#      
<CFELSEIF #day(now())# is "3" or #day(now())# is "13" or #day(now())# is "23">
      #day(now())#rd #MonthAsString(Month(Now()))# #Year(Now())#      
<CFELSE>
      #day(now())#th #MonthAsString(Month(Now()))# #Year(Now())#
</cfif>


</CFOUTPUT>
0
OeilNoirCommented:
Note that i used "Now()" Function as date, juste change it for whatever date variable you want to use.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

drstueyAuthor Commented:
Sorry OeilNoir but I am rejecting your answer because a) it is too complex for me to understand, I am just a beginner, and b) Helicopter posted theirs first and c) it is how to replace the now function that I didn't understand and you didn't explain how to, and Helipcopter did and d) its only 50 points so its not that important. Thanks for the info on how to include st, th, nd, but I've decided to keep it simple and just have the number.
0
drstueyAuthor Commented:
Sorry OeilNoir but I am rejecting your answer because a) it is too complex for me to understand, I am just a beginner, and b) Helicopter posted theirs first and c) it is how to replace the now function that I didn't understand and you didn't explain how to, and Helipcopter did and d) its only 50 points so its not that important. Thanks for the info on how to include st, th, nd, but I've decided to keep it simple and just have the number.
0
OeilNoirCommented:
Helicopter simply pasted DateFormat help of CFDOCS, which i assumed you already read. and now i don't see what is so complicated with :

#DateFormat(now(),"dd mmmm yyyy")#


0
MJacksonCommented:
The format is-

#DateFormat(yourdatevariable,"mmmm dd, yyyy')#

Will display as November 15, 1986

To put the th on 'th' or 'rd' on the end of the day is a bit trickier, and will require alot of conditional statements

Here is how I would do it-

<!-- Set Param to avoid unknown variable error -->

<cfparam name="daystring" default="">

<!-- Get the current day and set it in a variable. -->

<cfset #day# = #day(yourdatevariable)#>
<cfif #day# is 01 or #day# is 21 or #day# is 31>
  <cfset #daystring# ="st">
</cfif>
<cfif #day# is 02 or #day# is 22>
   <cfset #daystring# ="nd">
</cfif>
<cfif #day# is 03 or #day# is 23>
   <cfset #daystring# ="rd">
</cfif>
<cfif #daystring# is "">
   <cfset #daystring# ="th">
</cfif>

<!-- Now output your Date with the new variables -->


<!-- I assume this is a query with a date of 1986  :) -->

<cfoutput query="yourqueryname">
 
#DateFormat(yourdatevariable,"mmmm dd")##daystring#&nbsp;#DateFormat(yourdatevariable,"yyyy")#

</cfoutput>

Thats just how I would do it. But for the most part, I know theres about 8 different ways to do anything in CFML... Good luck!





0
OeilNoirCommented:
Thanks MJackson, but that's exactly what i wrote...
and it was too complicated for drstuey
0
MJacksonCommented:
Wow man, guess my head is still full of malted hops and bong resin from the weekend... I should have looked at the other answers... didn't mean to step on any toes man... sorry.
0
OeilNoirCommented:
hehe no problems, anyway that question was answered a while ago hehehe
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.