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
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With