Skip to main content
Participant
October 22, 2007
Question

get current year

  • October 22, 2007
  • 2 replies
  • 10513 views
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 has been closed for replies.

    2 replies

    Participant
    December 29, 2017

    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

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