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

I need to display the date and time values ​​in the application according to the user's local time zone. I would like my application to be able to automatically determine the user's time zone and then display the edited server date and time, respectively.

How do you do this in ColdFusion?

I am looking for a solution that DOES NOT prompt the user to choose their time zone at any time, such as Facebook.

thanks

+4
source share
4 answers

Instead of having CF do formatting, I would put forward a standardized representation of the date (for example, UTF (and JavaScript date format). Thus, the date is formatted to any time zone installed on the computer.

This allows you to avoid incorrect searches near zones of change of zones associated with summer savings compared to areas where this is not observed, and people traveling (formatting for "home time" compared to "local time").

Added:

Resets the current time in long format. This is a semi-off-cuff, so you want to look at the JavaScript Date Object 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> 
+10
source

ColdFusion is a server language. Therefore, you cannot determine the client’s time zone directly.

You can use client side JavaSript, find out the timezoneoffset settings and send it using 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 find the client’s country using the client’s IP address, but then you need to use an external service to get the country for a specific IP address, and you have to deal with problems such as daylight saving time and several time zones for the country .

+6
source

This would be faster and more accurate if you just asked your user to specify your time zone as part of your login profile; set once and forget.

Otherwise, you look at finding the IP address and determining where that IP address is, determining the time zone and then displaying the time, respectively. Even if you store information in a cookie or session variable, falsify it a bit when you can just ask your user what time zone they are in.

+1
source

if you use cfwheels.org and your users do not mind to see "yesterday" or "10 minutes ago" ... then TimeAgoInWords () is what you need.

-1
source

All Articles