CFLib.org – Common Function Library Project

composeDateTime([date][, year][, month][, day][, hour][, minute][, second])

Last updated May 3, 2013

author

Adam Cameron

Version: 1 | Requires: CF9 | Library: DateLib

Description:
Similar to createDateTime(), except all arguments are optional so one can be selective in which parts to specify for the result. Any unspecified arguments default to the current datetime's relevant component. This is really just a patch for a shortcoming in CFML's createDateTime() function. If you use this, perhaps vote to get an "official" version added to CFML: https://bugbase.adobe.com/index.cfm?event=bug&id=3374275

Return Values:
A date

Example:

defaultsWereSet = composeDateTime();
justYearWasSet = composeDateTime(2011);
monthWasSetToo = composeDateTime(2011, 3);
dayWasSetToo = composeDateTime(2011, 3, 24);
hourWasSetToo = composeDateTime(2011, 3, 24, 9);
minWasSetToo = composeDateTime(2011, 3, 24, 9, 30);
secWasSetToo = composeDateTime(2011, 3, 24, 9, 30, 00);
writeDump(variables);

Parameters:

Name Description Required
date The base date to start with. Defaults to now() No
year The year to set No
month The month to set No
day The day to set No
hour The hour to set No
minute The minute to set No
second The second to set No

Full UDF Source:

/**
 * Creates a date time from optional date parts
 * v1.0 by Adam Cameron
 * v1.1 by Adam Cameron (under guidance of GrumpyCFer). Adding date argument to allow for dates to not necessarily be now()-based. Fixed potential temporal issue.
 * 
 * @param date 	 The base date to start with. Defaults to now() (Optional)
 * @param year 	 The year to set (Optional)
 * @param month 	 The month to set (Optional)
 * @param day 	 The day to set (Optional)
 * @param hour 	 The hour to set (Optional)
 * @param minute 	 The minute to set (Optional)
 * @param second 	 The second to set (Optional)
 * @return A date 
 * @author Adam Cameron (adamcameroncoldfusion@gmail.com) 
 * @version 1, May 3, 2013 
 */
private date function composeDateTime(date date=now(), numeric year, numeric month, numeric day, numeric hour, numeric minute, numeric second){
	param name="arguments.year" default=year(date);
	param name="arguments.month" default=month(date);
	param name="arguments.day" default=day(date);
	param name="arguments.hour" default=hour(date);
	param name="arguments.minute" default=minute(date);
	param name="arguments.second" default=second(date);

	return createDateTime(arguments.year, arguments.month, arguments.day, arguments.hour, arguments.minute, arguments.second);
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson