Skip to main content
Participant
Oct 22, 2007 7:36 PM UTC
Question

get current year

Anyone created a function to get the current year? Here's what I've got so far and it appears to work:
function getCurrentYear() {
var datetime = now();
var month = year(datetime);
return Year(datetime);
}
Is there a better way to code such a function in cfml?
    This topic is closed to new replies. Start a new post to keep the conversation going.

    2 replies

    Participant
    Dec 29, 2017 10:12 AM UTC

    yes the best way is to use dateformat function. this function takes two arguments the first the date it self, and the second is the date format. in your case for example we can use that function such:

    <cfset month = #dateformat(now(), 'mm')#>the output will be 12 for decemeber

    this will get you the numeric portion of the month; you can also get fancy with it and get the letters by adding another m such as:

    <cfset month = #dateformat(now(), 'mmm')#>the output will be Dec for december.

    also coldfusion is rich with other datetime functions that can make life super easy i hope that helped

    Translate
    Participating Frequently
    Oct 22, 2007 7:39 PM UTC
    I actually wouldn't bother writing a user-defined function for it. #year(now())# is a perfectly reasonable way to reference the current year.
    Translate