Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Now() returns wrong hour 2

Status
Not open for further replies.

cumap

IS-IT--Management
Jul 9, 2007
268
0
0
US
Hi all,
here's my test
Code:
	nowTime = FormatDateTime(Now,3)
	response.write "NOW: " & nowTime & "<BR>"
	response.End()

my localhost return for ex: 9:00 AM, while testing with website host by Godaddy returns 10:00 AM (1 hour different).

Why? and how can I sync this.

thanks!
 
VBSCript is run on the server and therefore uses the time on the server.

The server is most likely in a different time zone that your local host.

I.E.
I'm in Vancouver, BC - my server's time is 10:25 AM.
I'm in New York - my server's time is 1:25PM





--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
Thanks vicvirk, I got that far... but does asp have anything automate instead of DATEDIFF to manually adjust the time to match with users from other parts of the US to login?

For instance:
I log in: 10:00 AM in CA
my friend: 12:00 PM in Lousiana
other: 1:00 PM in New York
 
instead of using "datediff" and through a little bit of trial and error the number "0.04167" equals approx "1 hour"

so you can add or deduct that number as follows for each hour

Code:
<% formatDateTime(now(),3) - .04167 %>

and multiples of .04167 to do more hours

Code:
<% formatDateTime(now(),3) - .08334 %>

If you are trying to display the user's current time, go with Javascript.


--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
Here is my new touch with js
Code:
<script>
function getCurrentTime() {
	var d = new Date();  // current server date/time
	var offset = d.getTimezoneOffset(); // difference in minutes between server time and UTC
	document.custLogin.la_tz.value = offset;
	return true;
}
</script>

so basically, the offset value will save to a hidden field in my login form and collected by this
Code:
        nowTime = Request.Form("la_tz")
	response.write "OFFSET: " & nowTime & "<BR>"
	response.write "NOW edit: " & DateAdd("h",nowTime,Now)

Result is not been changed/modified by the OFFSET value at all. What am I missing here?
 
by the way, I'd also tested in minute as well
Code:
response.write "NOW edit: " & DateAdd("n",nowTime,Now)
 
:) sorry I kept hit submit button nervously.

when I tried with minute "n", the returned hour is way off. NOT 1 HOUR fix like I expect it to be.
 
If you want to show the time based on the server time, you must adjust according to timezone. One way to do that with ASP only is asking the user in which TZ he/she is, store it in a cookie en presto:

(form1.asp)
Code:
<form action=form2.asp method=post>
What is your time zone?
<select name="fTZ">
<option value=+1>Amsterdam (+1)</option>
<option value=+1>New York (-5)</option>
<!-- etc -->
</select
<input type=submit value=OK />
</form>

(form2.asp)
Code:
<%
' Write cookie with timezone
Response.Cookies("MyTZ") = request.Form("fTZ")
%>
Timezone stored.<br />
<a href=form3.asp>Check the time</a>



(form3.asp)
Code:
<%
cTZ = Request.Cookies("MyTZ")
response.Write "Server time=" &  time & "<br>"
response.Write "Timezone = "  & cTZ & "<br>"
response.write dateadd("h", cTZ, now()  )
%>

But you can do it with Javascript DHTML, etc And i believe ASP.NET is better with timezones...



 
Thank foxbox!
I see what you're saying which complete what I mentioned earlier.

I'm currently learning js on the topic 'cause I do want the timezone to automatically adjust by itself for all user. If all fail, DATEADD will be my next best option. ASP.NET is not an option for now and particularly for this project.

Thanks again all for trying!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top