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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i have a function which returns a c

Status
Not open for further replies.

dustyfido

IS-IT--Management
Oct 1, 2000
12
US
i have a function which returns a clients time with respect to the server irrrespective of his system time. so if a client was accessing my page from new jersey and the server is hosted in arizona..with current time of 5.30 AM the client gets to see 7.30 AM irrrespective of what his system date is set to

i need to trap if a client is in a daylight savings zone.. anybody know how this is done in javascript??
 
Array.prototype.push = function(t)
{
this[this.length] = t;
}

//returns an array of seconds, minutes, hours, day, month, year, dayof week, day of year, daylight savings time boolean.
//in that order. function works fine for every day except the day that ends day light savings time. for the hour between 2 am and 2am
//on that day it will still say that it is daylight savings time... this is a bug that i was unable to work out.
Date.prototype.localtime = function()
{
var x = new Array();
var n = 0;
x.push(this.getSeconds());
x.push(this.getMinutes());
x.push(this.getHours());
x.push(this.getDate());
x.push(this.getMonth()+1);
x.push(this.getFullYear());
x.push(this.getDay());
var feb = (!(x[5]%4) && !(!(x[5]%100) && (x[5]%400)))?29:28;
switch(x[4])
{
case 1:
n = x[3];
break;
case 2:
n = 31+x[3];
break;
case 3:
n = 31+feb+x[3];
break;
case 4:
n = 62+feb+x[3];
break;
case 5:
n = 92+feb+x[3];
break;
case 6:
n = 123+feb+x[3];
break;
case 7:
n = 154+feb+x[3];
break;
case 8:
n = 184+feb+x[3];
break;
case 9:
n = 215+feb+x[3];
break;
case 10:
n = 245+feb+x[3];
break;
case 11:
n = 276+feb+x[3];
break;
case 12:
n = 306+feb+x[3];
break;
}
x.push(n);
var s = "04"
var t = "01";
var m = new Date(s+"/"+t+"/"+x[5]+" 02:00:01");
if(m.getDay() != 0)
{
t = 8-m.getDay();
t = "0"+t;
m = new Date(s+"/"+t+"/"+x[5]+" 02:00:01");
}
s = "10";
t = "31";
var f = new Date(s+"/"+t+"/"+x[5]+" 02:00:01");
if(f.getDay() != 0)
{
t = 31-f.getDay();
t = "0"+t;
f = new Date(s+"/"+t+"/"+x[5]+" 02:00:01");
}
if((this > m) && (this < f))
{
x.push(1);
}
else
{
x.push(0);
}
return x;
} luciddream@subdimension.com
 
hi lucid,
thanks for the code.....but i missed out something in the post..i need to be able to figure out if the client has daylight savings..if i understand correctly the above code calculates daylight savings based on the month and not on the client..
i have a problem diffrentiating client access from nj and client access from say japan or india..
i have to be able to figure out if a client is accessing from a zone which is in daylight savings and only then implement daylight savings..
thanks,
 
it does calculate by the clients machine time.... assuming the client has their clock set properly. and, it checks by the day and time, it works for every time except the hour that ends daylight savings time. luciddream@subdimension.com
 
hi lucid,
just an update..my problem was figuring out the clients time with respect to the servertime..i pass the server time and its offset from GMT to a javascript function and the function calculates the clients offset from GMT and displays the clients time irrespective of the clients mc settings..
so for example the client could have his clock set to 1995 june6 but the date he gets to see when he logs in to the site would still be as it should be in the client's timezone.
the client would be able to set his clock from the site if he wishes.

i was using javascript Date objects 'getTimezoneOffset()'function to calculate clients offset..the documentation states that this is never constant and returns the clients correct offset including offset.. it works fine now.. but seemed to have a roll over problem on the 1'st of april
 
The code by luciddream looks beautiful - I had no idea that Javascript can handle this kind of stuff - switch, push, =function(), etc. - in fact, I would like to know how it works because I am quite new at this.

Would it be possible to have a sample web page to illustrate the whole thing?

One has to love this forum. Such dedication.
 
&quot;Would it be possible to have a sample web page to illustrate the whole thing?&quot; --> you mean, a page to explain how jscript works ? then the best ever are : msdn if you target ie, devedge if you target ns, w3c if you want to conform to standards, and some cool ones for cross browser coding are referenced in the faq area here
 
No, iza, I am NOT looking for an explanation how Javascript works, just the <BODY> part of a web page that illustrates what luciddream's code does.
 
ANY web page would fit for this !!! this function only returns &quot;an array of seconds, minutes, hours, day, month, year, dayof week, day of year, daylight savings time boolean.in that order&quot; - you can call it form any place, for instance &quot;<input type=text value=&quot;javascript:my_date.localtime[0]>&quot; or anything !!!!
 
Thank you, iza. What throws me is the expression &quot;something=function() {...&quot;

So far, I have only seen &quot;function something() {...&quot;

Am I being naive?
 
hi lucid,
any reason why you prefer not to use
var dt = new Date();
month = dt.getMonth();
day = dt.getDay() ;
etc......
why implement something that is already implemented for you by the underlying dom?
just curious..

 
engcomp, this adds a new function to the prototype of the objet. More on this on some faqs here, or follow the ref sites linked in the faqs as well - a good site also is dustyfido, i guess your functions don't return a daylight saving boolean - was he purpose here ;-)
 
i do use the getMonth() method.... see

x.push(this.getMonth()+1);

luciddream@subdimension.com
 
hi iza,
the date objects getTimeZoneOffset() never returns the same offset through out the year
eg:
if you are accessing from nj on the 5'th of march
it will return a value of 300 ( thats 5 hours from GMT)
on april 5 it retruns 240 ( thats 4 hours from GMT )
it takes care of daylight savings thats the beaut..

so my point is if we are writing code for daylight savings thats already taken care of by the unerlying dom..

the only problem i had was that on the 1'st of april it din't seem to work..it might be that it has a roll over problem or that i din't close the explorer window and start a new instance
any ways since the 2'nd of april its been working fine..
and all it takes is 5 lines of code?? and i don't depend on the clients mc settings..
irrrespective of the clients mc setting i still display the right time on the client for his timezone..








 
of course it depends on the clients machine settings... how do you think it knows what timezone its in? luciddream@subdimension.com
 
also... IE seems to have a problem with getTimeZoneOffset()

for some inane reason they decided to implement it as

getTimezoneOffset()

lowercase &quot;z&quot;.... luciddream@subdimension.com
 
hi lucid,
the way it works is i send the servers current time and the servers offset from GMT .. on the client side i get the clients current time and its offset from GMT..
i calculate the total offset between client and server and calculate the date and time of the client with respect to the server..
i also get the clients current time and do a diff on the clients actual time and the time as calculated in the above step...this is the variable i use to do all other date calculations on the browser so..irrrespective of the clients mc settings i always have the right time showing up on the browser

i stand corrected on the spelling..but apart from the layover problem on the 1'st of april..i have'nt had any problems so far..and the site is being tested by QC sitting in india and by clients in central time...
so far its holding up..will post again if a problem is detected...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top