Forum Moderators: open
im having some problems .. for a change..
this time with dates.
i have a little function ..
function dayOfYear(osD,selD){
todayTime = selD.getTime();
startTime = osD.getTime();
DoY = (todayTime - startTime)/86400000;
if (DoY%1!= 0){
DoY = DoY-(DoY%1);
}
DoY += 1;
return DoY;
}
this is all to do with timetables*..
i have a offset date (osD) and a first of the month date (selD), both passed to this function in order to work out what day of the year it is compared to the offsetdate. this allows me to pick up part of a string starting at this 'DoY' and going for the amount of days in the month.
ok, got that so far.
now my problem.
as most of you will know in Nov the clocks change. spring forward, fall back and all that.
problem is the date objects i have been passing were simply :
osD = new Date(year, month, day)
this gave me problems , because it works out the date at midnite, and the DST clock stuff was throwing the DoY off by one day , in Nov only. (and April too is it).. nonetheless. i then added a '12' on the end to get the date at middday .. thinking that then the 1 hr either way wouldnt matter..
how wrong i was.. :(
any ideas?
* timetable is a string as follows.
.^ <--offsetdate...^ <--first of month in question Nov for now..)
'AAAAAAAAAAAAGGGGGGXGGDDDDDDDDGGGGGGGGDDDDDDDDDDDD'
ie for Nov 30 days worth of that starting there would be..
'XGGDDDDDDDDGGGGGGGG.....
[edit] ps if you are wondering why the +1 , its because my timetable is zero based...
still, any other ideas will be gratefully recieved..
I've run JavaScript clocks before, and sat there watching it as it went from normal time to DST - and it worked...
<script type="text/javascript">
blah = new Date();
document.write(blah);
</script>
Note how it makes a difference between, say, EST and EDT.
The easiest way to solve this problem is to convert both time stamps to UTC. (UTC doesn't change, DST or not). So, compare the times in UTC instead :)
blah = new date(year, month, day[,hr])
if i do june for instance and alert blah i get..
sun jun 5 00:00 UTC 2003
but for nov , when creating the date in the same way i get
sat nov 5 00:00 UTC+01:00 2003
see what i mean?
and when i take one from the other with the function in the first part of the post i get a day out from nov to march..
so i presume that both these dates are UTC.. so what can i do..
like i said ive simply done
function dayOfYear(osD,selD){
var theMonth;;
todayTime = selD.getTime();
startTime = osD.getTime();
DoY = (todayTime - startTime)/86400000;
if (DoY%1!= 0){
DoY = DoY-(DoY%1);
}
theMonth = selD.getMonth()
if (theMonth == 10 ¦¦ theMonth == 11 ¦¦ theMonth == 0 ¦¦ theMonth == 1 ¦¦ theMonth == 2) {
//dont do nuffink
}else{
DoY += 1;
}
return DoY;
}
so any one like to explain some more?!?!?!?!?
cheers all
nat
i make 2 javascript dates with the date function
this all works fine, untill i create a date that is in GMT (british winter time?)
ill try to excplain again , as this is still a problem sadly..
you work your butt off, go on a short holiday, and come back.. and noone has done your work for you ;)
the dates were created in the following way
blah = new date(year, month, day[,hr])
2 dates one comes out as
if i do june for instance and alert blah i get..
sun jun 5 00:00 UTC 2003
but for nov , when creating the date in the same way i get
sat nov 5 00:00 UTC+01:00 2003
its the +01:00 that seems to be screwing things up.
when i take one date from the other it gets a day out of step..
am i explaining this that badly?
cheers,
nat
maybe i should explain the problem again..
i have two dates..
i need to find out the number of days between them.
so i tried the function i wrote above.. this seemed to do fine until the date i was using crossed into our daylite saving time.. which was signified as a +01:00 when i alerted the date value.
this screwed things so that was a day out for these months..
which is no good, so i added the biut where it doesnt add a 1 for these months.. gets the index coorrect then.
but im not happy with this solution ..
any ideas..
thanks again
sorry if this is old ground and im being stoopid.