• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Date/Time Functions

Date/Time Functions

Handling date and time variables can be somewhat tricky, especially when you're doing SQL calls, in which case they have to be in a format that ODBC can interpret correctly. CFML supports a number of functions that operate on date and time variables. The following list provides a sampling of these functions. If you specify a year quantity less than 100, it is interpreted as a twentieth century value.

  • CreateDateTime(year,month,day,hour,minute,second)— Creates a date/time variable with the value corresponding to the year, month, day, hour, minute, and second information provided. For example, to specify 12 noon on July 4, you would write

    <cfset independenceDay = CreateDateTime(2002,7,4,12,0,0)>
    						
  • CreateODBCDateTime(year,month,day,hour,minute,second)— Creates a date/time variable in ODBC format with the value corresponding to the year, month, day, hour, minute, and second information provided. You should use ODBC-formatted dates in your SQL statements. The ODBC-formatted output from the following code would be {ts 2002-07-04 12:00:00}:

    <cfset independenceDay = CreateODBCDateTime(2002,7,4,12,0,0)> 
    <cfoutput>#independenceDay#</cfoutput>
    						
  • DateCompare(datetime1,datetime2)— Compares datetime1 and datetime2 and returns -1 if datetime1 occurs before datetime2, 0 if datetime1 equals datetime2, and 1 if datetime1 occurs after datetime2. The output from the following code would be -1:

    <cfset christmas = CreateODBCDateTime(2002,12,25,0,0,0)> 
    <cfset newYearsDay = CreateODBCDateTime(2003,1,1,0,0,0)>
    <cfoutput>#DateCompare(christmas,newYearsDay)>#</cfoutput>
    
  • Day(date)— Returns the day of the month (1–31) on which that date falls. For example:

    The current day of the month is <cfoutput>#Day(Now())#</cfoutput>.
    						
  • Hour(date)— Returns the hour (0–23) specified by date. For example:

    The current hour on the system clock is <cfoutput>#Hour(Now())#</cfoutput>.
    						
  • IsDate("date")— Returns TRUE if date is a valid date/time variable or FALSE if date is not. The following sample code would print a value of YES, the string equivalent of a Boolean value of TRUE:

    <cfoutput>#IsDate("September 29, 2002")#</cfoutput> 
    
  • IsLeapYear(year)— Returns TRUE if year is a leap year or FALSE if year is not. The following sample code would print a value of NO, the string equivalent of a Boolean value of FALSE:

    <cfoutput>#IsLeapYear(2002)#</cfoutput> 
    
  • Minute(date)— Returns the value of the minutes (0–59) specification in the variable date. For example:

    The current number of minutes on the system clock
    is <cfoutput>#Minute(Now())#</cfoutput>.
    						
  • Month(date)— Returns the numeric value of the month (1–12) in the variable date. For example:

    The current month is <cfoutput>#Month(Now())#</cfoutput>.
    						
  • Now()— Returns the current system date and time in ODBC format. For example:

    The current date and time is <cfoutput>#Now()#</cfoutput>.
    						

    TIP

    The Now() function makes it easy for you to time stamp database fields such as DateEntered or LastUpdated.


  • ParseDateTime(string)— Converts the string string to a valid date/time variable in ODBC format. For example:

    Your birthday in ODBC format is
    <cfoutput>#ParseDateTime(Form.birthday)#</cfoutput>.
    						
  • Second(date)— Returns the value of the seconds (0–59) specification in the variable date. For example:

    The number of seconds on the system clock is
    <cfoutput>#Second(Now())#</cfoutput>.
    						
  • Week(date)— Returns the numerical week of the year (1–53) during which date occurs. For example:

    Christmas occurs during week number <cfoutput>#Week("December 25,
    2002")#</cfoutput>. 
    
  • Year(date)— Returns the value of the year specification in the variable date. For example:

    The current year on the system clock is <cfoutput>#Year(Now())#</cfoutput>.
    						

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint