CFLib.org – Common Function Library Project

timeZoneNow([timeZone])

Last updated October 2, 2014

author

Ray Ford

Version: 1 | Requires: CF9 | Library: DateLib

Description:
E.g. Your server is in the US & your target audience is in the UK. This will return the current date/time as it would appear if the server was located in the UK. Daylight saving time is also considered. If you provide an invalid time zone or an empty string, the function will throw an error and show about 600 time zones to choose from.

Return Values:
Returns a date/time.

Example:

Current date/time in Hawaii
<br />
<cfoutput>#timeZoneNow('US/Hawaii')#</cfoutput>
<br /><br />
Current date/time in Amsterdam
<br />
<cfoutput>#timeZoneNow('Europe/Amsterdam')#</cfoutput>

<!--- Show all the timezones to choose from inside an error
<cfoutput>#timeZoneNow('')#</cfoutput> --->

Parameters:

Name Description Required
timeZone Time zone. No

Full UDF Source:

/**
 * Show the current date/time for a given time zone.
 * 
 * @param timeZone 	 Time zone. (Optional)
 * @return Returns a date/time. 
 * @author Ray Ford (fordray+cflib@gmail.com) 
 * @version 1, October 2, 2014 
 */
function timeZoneNow(timeZone) {
	var loc={};
	loc.GMT = DateAdd( "s", GetTimeZoneInfo().UTCTotalOffset, Now() );
	loc.ObjTimeZone = createObject("java","java.util.TimeZone").getTimeZone(timeZone);
	if ( ListFind( arrayToList( loc.ObjTimeZone.getAvailableIDs() ),timeZone) EQ 0) {
		throw(message='Invalid Time Zone',detail = arraylen(loc.ObjTimeZone.getAvailableIDs()) & ' Timezones: <br />' & arrayToList(loc.ObjTimeZone.getAvailableIDs(),' '));/* this line wont work in CF8 */
	};
	loc.StdTime = DateAdd( "s",  loc.ObjTimeZone.getRawOffset() / 1000, loc.GMT );
	loc.DsTime = DateAdd( "s",  loc.ObjTimeZone.inDaylightTime(loc.StdTime) * loc.ObjTimeZone.getDSTSavings() / 1000, loc.StdTime );
	return loc.DsTime;
}
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