Avatar of hc1619
hc1619

asked on 

How to get the time & date in a different City

I have a server in the US, in Texas.  I have a website on there that operates primarly in the Australian market.  I don't want to change the server time on the box itself (as other things run on there).  What's a good piece of code that will tell me the date & time in proper format in a given timezone (Sydney, Australia) which also takes into account DST on both the host box and in the timezone I'm finding?
Web ServersColdFusion Language

Avatar of undefined
Last Comment
judsonmusic
Avatar of Member_2_2537100
Member_2_2537100
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of judsonmusic
judsonmusic
Flag of United States of America image

I always do #timeFormat(dateAdd('h', 1, now()), 'hh:mm tt')# the '1' would be the hours offest from where you are.

For example. I am on the east coast and have to use this all the time because my server is in the mid west which is an hour behind.

I personally like setting a variable like <cfset currentTime = timeFormat(dateAdd('h', 1, now()), 'hh:mm tt')>

Judson
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

You can get the Client time only from browser by JavaScript.
Therefore you can fetch that value first AFTER browser page submit.
I wouls suggest to place that client side value into a Cookie.
Like this:
<cfset tzOffset=0 >
<cfif isDefined("cookie.tzOffset") >
  <cfset tzOffset=cookie.tzOffset>
</cfif>
<cfoutput>
ServerTime: #Now()#<br>
ClientTime: #DateAdd("n",tzOffset,Now())#
</cfoutput>
<script>document.cookie="tzOffset="+(new Date()).getTimezoneOffset()+";path=/;";</script>

Open in new window

Avatar of Zvonko
Zvonko
Flag of North Macedonia image

If you need that tzOffset on multipole pages then is placing the line 9 from above into application.cfm/cfc perhaps the better choice.
Or even the complete client time calculation as demonstrated above.

Avatar of judsonmusic
judsonmusic
Flag of United States of America image

If you need to get the time based on certain events happening you may need to use scripting. Coldfusion will only generate it on inital page load or a submission
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of judsonmusic
judsonmusic
Flag of United States of America image

Wow. My solution just more variablized I guess, but the cookie wouldn't work in some cases.
Web Servers
Web Servers

A web server refers to the software that helps to deliver web content that can be accessed either through the Internet or through an intranet. The primary function of a web server is to store, process and deliver web pages to clients. The communication between client and server takes place using the Hypertext Transfer Protocol (HTTP). The most common use of web servers is to host websites, but there are other uses such as gaming, data storage, running enterprise applications, handling email, FTP, etc.

33K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo