homepage Welcome to WebmasterWorld Guest from 54.196.195.158
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
dates going awry in JS
natty




msg:1489787
 4:45 pm on Apr 15, 2003 (gmt 0)

hi again all,

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.....


each letter referring to a different something.
but as i tried to explain above, it all goes pear shaped in the months that the clocks change in...

[edit] ps if you are wondering why the +1 , its because my timetable is zero based...

 

le_gber




msg:1489788
 4:59 pm on Apr 15, 2003 (gmt 0)

HI,

Javascript is based on the the viewer computer date/hour. The correct date should therefore be passed to your function.

Or may be I don't understand your problem?

Leo

natty




msg:1489789
 5:14 pm on Apr 15, 2003 (gmt 0)

i dont think you do quite.. but its very up its own arse anyway..
the date n the pc doesnt reallyt matter, its simply the dates passed to the function and what it returns that is important. ie : the number of days inbetween the selD and the osD.
i decided to simply not add the 1 for jan, feb, mar, nov, dec.
bit poo, but im in a hurry ;)

still, any other ideas will be gratefully recieved..

DrDoc




msg:1489790
 2:54 am on Apr 16, 2003 (gmt 0)

If the date is "read" the instant the function is executed it should return the correct date/time/etc no matter what.

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 :)

natty




msg:1489791
 9:29 am on Apr 16, 2003 (gmt 0)

im not quite sure what you are saying dude..
both dates are generated by simply..

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

g1smd




msg:1489792
 11:55 pm on Apr 16, 2003 (gmt 0)

>> sun jun 5 00:00 UTC 2003 <<

Time zone is UTC, so this is 2003-06-05 00:00 UTC.

>> sat nov 5 00:00 UTC+01:00 2003 <<

Time Zone is UTC+0100, so this is 2003-11-04 23:00 UTC.

DrDoc




msg:1489793
 3:39 am on Apr 17, 2003 (gmt 0)

Why aren't you using the builtin JavaScript function Date()?

blah=new Date();

If you document.write(blah) it will print out the current date and time.

You have a ton of functions you can use:

getDate()
detMonth()
getYear()
...
setHours()
setMinutes()
...

natty




msg:1489794
 9:57 am on Apr 23, 2003 (gmt 0)

i am using the javascript date function.
as i attempted to explain.

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

DrDoc




msg:1489795
 10:12 am on Apr 23, 2003 (gmt 0)

I'm sure this page will prove helpful: The Date() object [developer.netscape.com]

DrDoc




msg:1489796
 10:12 am on Apr 23, 2003 (gmt 0)

Especially look at the UTC functions on the page...

natty




msg:1489797
 12:21 pm on Apr 23, 2003 (gmt 0)

it seems either i am missing something .. or you are.. i cant work it out.
the UTC methods are for existing dates yes..
and all dates are to be constructed as follows..
var myDate = new Date(yr, mnt, day, time ....);
which is exactly what i am doing with my two dates.

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.

g1smd




msg:1489798
 10:49 pm on Apr 23, 2003 (gmt 0)

>> sun jun 5 00:00 UTC 2003 <<

Time zone is UTC, so this is 2003-06-05 00:00 UTC.

>> sat nov 5 00:00 UTC+01:00 2003 <<

Time Zone is UTC+0100, so this is 2003-11-04 23:00 UTC.

There is your one day error; it isn't an error at all.

natty




msg:1489799
 8:49 am on Apr 25, 2003 (gmt 0)

if that is so , then why does it do it when i do a time of 5 pm or the like..
then an hr either way doesnt make any difference at all.
?!?!?!?!?!

DrDoc




msg:1489800
 1:25 am on Apr 27, 2003 (gmt 0)

Can you post a brief JS example. Also, remember that the Date() function should be capitalized, hence "Date()", not "date()". Don't know if that would make a difference or not...

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved