So it has been a while since I've done a CF 101 post, but for those who do not remember, these are entries specifically targeting people brand new to ColdFusion. For most of my readers, this will probably be something you already know, but on the other hand, I've never been surprised by own my lack of knowledge, so maybe it will be helpful as well.
This was an old question sent to the Ask a Jedi form:
Hello, I followed the code you have written to the T and thankfully it works. My question is how do you get the daysoftheweek showing as 'Sun' 'Mon' etc. instead of Sunday, Monday...There are a few ways to get the day of the week. The simplest is with the dayOfWeekAsString() function:
<cfoutput>#dayOfWeekAsString(dayOfWeek(now()))#</cfoutput>
This will get the day of the week value (a number from 1 to 7) and pass it to the dayOfWeekAsString function. So how could you get it to be Sun instead of Sunday? Just use the left() function.
<cfoutput>#left(dayOfWeekAsString(dayOfWeek(now())),3)#</cfoutput>
The only issue with that is that you need to be careful if you aren't using English as your current locale. The dayOfWeekAsString function is automatically localized in CFMX7. In other languages, the first 3 characters may not make sense.
The other way to format the day of the week is with the dateFormat() function. It lets you pass several tokens to format a date. Two tokens refer specifically to the day of the week. This code will return just the day of the week:
<cfoutput>
#dateFormat(now(),"dddd")#
</cfoutput>
To get a 3 letter day of the week, you can simply use the "ddd" token instead.
Check the docs for more information on the tokens you can use in dateFormat, as they changed in MX.
You will notice on my calendar pod to the right, I use just the first letter. This is done using the left() function as described above.
One last tip - and this is something that I tend to forget doing most of the time so you will have to pardon me if you notice that you don't actually see it in my code. I suggest folks store their date and time masks as application variables. That way, site wide, you can set what you want to use for a date and time format. Maybe you want "08/03/2006". Maybe you want "August 3, 2006". The point is - if you store the mask as an application setting, it is easy to update in the future. In fact, you can write a simple UDF to do it for you. That way instead of coding:
#dateFormat(thedate, application.mask)#
You can do:
#myDateFormat(thedate)#
Even better, you can automatically do both date and time formatting:
#myDTFormat(thedate)#
So now that I'm seriously off topic from the original post, I'll stop.
Archived Comments
Ray,
You are definitely right. The best way to help others is to start at the beginning. We get caught up trying to create the next big mouse trap that we forget small ways to make our lives easier.
Also, I never get tired of reviewing simple topics and sharing them. You learn the most about any structured language when you try to teach it to someone else. The less experienced people ask great questions that you take for granted.
I believe the CF blogging community is centered upon this shared theme. There are exceptions, but sharing and teaching is noticeable everywhere.
I have international users for one of my applications and allow the user to specify a locale. I store the locale information in session scope and use LSDateFormat, etc, to display dates and numbers in a format more familiar to the user.
Don't forget the CF Cookbook!
Here's a fun UDF for conditionally formatting dates. I use this for query columns that may or may not be dates (hey, i didn't create the database, i just have to live with the structure).
<cffunction name="ifDateFormat" access="private" output="false" returntype="any">
<cfargument name="str" required="true" type="any" hint="the string to check - could be a date, could not be....">
<cfargument name="mask" required="true" type="string" hint="the mask to apply if it is a date">
<cfset var retStr = "">
<cfif isDate(arguments.str)>
<cfset retStr = dateFormat(arguments.str, arguments.mask)>
<cfelse>
<cfset retStr = arguments.str>
</cfif>
<cfreturn retStr>
</cffunction>
arguments.mask? Never heard of this . . .
arguments.mask is the name Todd used for his second argument to the UDF. Also, "mask" is the term used in the documentation for date and time format. It is the "format string" that determines how to display the date and time. You will notice I didn't use the term "mask" since I don't think it is a great word for it.
I miss the old design, I didn't want to say it but I miss it.
It will take me a while until I get used to this new design.
Is not bad, but I was so used to the old one that I almost close this window twice seeing that I didn't recognize the graphics, colors, etc.
Fernando: Change is good. :) You will get used to it.