CFLib.org – Common Function Library Project

timeZoneNow([timeZone])

Last updated October 02, 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;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson