DateFormat() Add Day Shortcut

Posted: January 21, 2013 in ColdFusion
Tags: , , ,

I ran into some old code that adds 7 days to a date and spits it out in a DateFormat().

Normally you’d do this:

dateFormat( dateAdd( 'd', 7, now() ), 'mm/dd/yyyy' );

But the code I ran into doesn’t use the dateAdd() method. It just simply adds days.

dateFormat( now() + 7, 'mm/dd/yyyy' );

Though I can’t find this shortcut documented anywhere it seems to be working well with at least ColdFusion v8 and v9.

Advertisements
Comments
  1. Ryan Stille says:

    This is interesting. I think whats happening here is ColdFusion is converting the output of now() to an integer so it can be added with 7.

    DateFormat happens to accept an integer representation of a date, starting at the Epoch (12/30/1899). For example DateFormat(0,”long”) will give you “December 30, 1899” (more about this here: http://en.wikipedia.org/wiki/Epoch_(reference_date)#Computing )

  2. Andrew Scott says:

    Yeah what Ryan said, this is not a shortcut as every single ColdFusion function that returns data can do this. Because the function is evaluate and then the add is then calculated like any normal expression.

    But be careful, the 7 being added might not work the way you would expect. For example, how would you add 10 mins or 30 hours to the now?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s