[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

DateFormat syntax

Posted on 2000-01-09
10
Medium Priority
?
828 Views
Last Modified: 2013-12-24
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
0
Comment
Question by:drstuey
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 8

Accepted Solution

by:
Helicopter earned 200 total points
ID: 2337695
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
 
LVL 1

Expert Comment

by:OeilNoir
ID: 2337866
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
 
LVL 1

Expert Comment

by:OeilNoir
ID: 2337869
Note that i used "Now()" Function as date, juste change it for whatever date variable you want to use.
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 

Author Comment

by:drstuey
ID: 2340583
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
 

Author Comment

by:drstuey
ID: 2340584
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
 
LVL 1

Expert Comment

by:OeilNoir
ID: 2342241
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
 

Expert Comment

by:MJackson
ID: 2368777
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
 
LVL 1

Expert Comment

by:OeilNoir
ID: 2368786
Thanks MJackson, but that's exactly what i wrote...
and it was too complicated for drstuey
0
 

Expert Comment

by:MJackson
ID: 2368805
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
 
LVL 1

Expert Comment

by:OeilNoir
ID: 2368816
hehe no problems, anyway that question was answered a while ago hehehe
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Media Temple is thrilled to announce the launch of our new Partner Program, specifically designed to empower digital agencies and adtech platforms by offering white-glove support and exclusive hosting enhancements to optimize their sites and their c…
Media Temple is proud to announce our partnership with the Society of Digital Agencies (SoDA) as their exclusive hosting partner.
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question