Ask a Jedi: Formatting a year to just 08?

Robert asks:

Ok.. I'm doing : #Year(Now())# to get just the current year. It returns 2008.. great.. How do I get it to return just 08? I've tried: #Year(Now(),"yy")# #Year(Now(),"y")# But both fail. Any suggestions for the stupid?

You aren’t stupid. You just overthought it a bit. ;) Like most things in ColdFusion, you have multiple ways to do this. One way is with the simple Right() function:

<cfset thisyear = year(now())> <cfset oyear = right(thisyear,2)> <cfoutput>#oyear#</cfoutput>

You could write that all in one line of course. Another way to do it with dateFormat, and that’s the form I’d use:

<cfoutput>#dateFormat(now(), "yy")#</cfoutput>

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments