Using Functions to Work with Dates

 < Day Day Up > 



Another commonly used function in the ColdFusion MX toolbox is Now(), a unique function that requires no input. It simply generates a ColdFusion date/time object containing the current date and time on your server. However, Now() is almost always used in conjunction with other functions. By itself, it's not that useful as you'll see if you place it in one of your templates as is:

<cfoutput>#Now()#</cfoutput>

If you load the page in your browser, you see the current date and time on your server, but in a format that isn't exactly ready to plug into your site design:

{ts '2004-08-16 18:04:24'}

The reason is that ColdFusion date/time objects are designed to work in conjunction with other functions. For example, if you simply want to display a date at the top of your ColdFusion MX templates, you use Now() as the input for another common function, DateFormat():

<cfoutput>#DateFormat(Now())#</cfoutput>

Note the syntax: Now() is enclosed within the parentheses of DateFormat().

Note 

Almost all ColdFusion MX functions can be nested within one another. Although there's no limit to the number you can nest, keep in mind that functions nested more than five or six levels begin to cut into the loading time of your templates.

Viewed in a browser, this combination produces something like this:

16-Aug-02

It's beginning to look more like a date you'd expect to see in a Web page, but it still needs a little work. Fortunately, DateFormat() enables you to include an optional mask as input. A mask is a representation of the way you want the date to display, using identifying strings like "mmmm" to signify that you want the month to display as a long name (as opposed to a number, and so on). You enclose the mask in quotes and set it off with a comma:

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

The preceding line produces output that's much more useful in the average Web page:

August 16, 2002

Using a different mask, you could also use Now() to display only the current month:

<cfoutput>#DateFormat(Now(), "mmmm")#</cfoutput>

The preceding line would simply display August assuming that's the current month on your server.

To display the current time, you can use another function called TimeFormat(), again using Now() as input (remember that Now() holds both the current date and the current time). TimeFormat() also accepts a mask as input:

<cfoutput>      #DateFormat(Now(), "mmmm dd, yyyy")#<br>      #TimeFormat(Now(), "h:mm tt")# </cfoutput>

The previous code produces output like this:

August 16, 2002 6:29 PM
Tip 

You'll find a complete list of mask characters and details on how to use them in your ColdFusion MX documentation, under the sections covering the DateFormat() and TimeFormat() functions.

Tip 

For international sites that need to display dates and times in non-U.S. standard format you can use the LSDateFormat() and LSTimeFormat() functions. Refer to the ColdFusion MX documentation for more information on these functions



 < Day Day Up > 



Macromedia Studio MX Bible
Macromedia Studio MX Bible
ISBN: 0764525239
EAN: 2147483647
Year: 2003
Pages: 491

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net