Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display date and time according to the user's time zone in Coldfusion

I need to display datetime values in my application according to the logged in user's local timezone. I would like my app to be able to detect the user's timezone automatically then display the adjusted server date and time accordingly.

How do you do this in Coldfusion?

I am looking for a solution that does NOT prompt the user to select their time zone at any point, like Facebook.

Thanks

like image 632
Paolo Broccardo Avatar asked Dec 03 '22 10:12

Paolo Broccardo


2 Answers

Instead of having CF do the formatting, I would push a standardized date representation (like UTF( and have JavaScript format the date. That way, the date is formatted to whatever timezone is set on the computer.

This avoids wrong lookups near zone change areas, dealing with daylight savings vs. areas where it isn't observed, and people traveling (formatting to "home time" vs. "local time").

Added:

This dumps the current time in a long format. It's semi-off-the-cuff, so you'll want to look at the JavaScript Date Object's methods for more formatting options.

<cfset n = dateconvert("local2utc",now())>
<script>
 d = new Date();
 d.setUTCFullYear(#dateformat(n, "yyyy")#);
 d.setUTCMonth(#dateformat(n, "m")#);
 d.setUTCDate(#dateformat(n, "d")#);
 d.setUTCHours(#timeformat(n, "H")#);
 d.setUTCMinutes(#timeformat(n, "m")#);
 document.write(d.toLocaleString());
</script>
like image 60
Ben Doom Avatar answered Dec 28 '22 12:12

Ben Doom


ColdFusion is a server side language. So you can't determine the timezone of a client in a direct way.

You can use JavaSript on the client side, find out the timezoneoffset and send it via an Ajax request to ColdFusion.

JavaScript:

var clienttime = new Date();
var time = clienttime.getTimezoneOffset()/60;

Then on the server side you can use the offset to calculate the date you want to show the user.

You can also lookup the country of the client using the client IP, but then you have to use an external service to get the country for a certain IP and you have to deal with problems like daylight savings and multiple timezones per country.

like image 20
Andreas Schuldhaus Avatar answered Dec 28 '22 14:12

Andreas Schuldhaus