...

View Full Version : Some questions on dates



windy
02-18-2007, 07:41 AM
I have a few questions related to dates in order of importance (to me):

- What is the best way to handle dates/times that specifically ignores daylight savings, time zones etc. I'm not sure if UTC is affected by DST.

- How is javascript and/or its implementations affected by the change in North America to a different daylight savings schedule starting this March 2007?

- What is the best way to calculate the number of weekdays between two dates? I have been simply looping between the dates with a counter and only incrementing the counter when getDay() != 0 && getDay() !=6. For longer time spreads this could be quite inefficient.

Thanks, Windy

Kor
02-18-2007, 08:37 AM
1 - 2 : Javascript returns the date of the user's computer, so that it is dependent on the way user has set it's computer's date. If you need a precise reference, use a server-side application to have a single reference. Even so, when you will use javascript javascript Date() object later, the operation will follow again the user's computer behavior (DST...)

3. You may use Date() object and it's method to subtract two days. The result is measures in milliseconds, so that you may simply divide it by 1000X60X60X24X7 so find the weeks' difference.

Philip M
02-18-2007, 08:42 AM
- What is the best way to handle dates/times that specifically ignores daylight savings, time zones etc. I'm not sure if UTC is affected by DST.


UTC is in effect Greenwich Mean Time and is not affected bt DST.



- What is the best way to calculate the number of weekdays between two dates? I have been simply looping between the dates with a counter and only incrementing the counter when getDay() != 0 && getDay() !=6. For longer time spreads this could be quite inefficient.


See for example:-
http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CC30078F008

windy
02-18-2007, 09:02 AM
3. You may use Date() object and it's method to subtract two days. The result is measures in milliseconds, so that you may simply divide it by 1000X60X60X24X7 so find the weeks' difference.

Thanks, Kor. Actually, I was probably not clear enough in my OP. I meant the number of weekdays (MTWTF) or, how it is sometimes expressed - the number of working days in a given period.




See for example:-
http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CC30078F008

Thanks, Philip. The reason I posted this question in the first place was that I had been using that example you referenced and have been getting some strange results.

Try...

startDate=new Date(2007,1,15);
endDate=new Date(2007,2,15);
document.write(weekdaysBetween(startDate,endDate));


Windy

Philip M
02-18-2007, 10:30 AM
A search of Google found;-

http://lawrence.ecorp.net:32993/inet/samples/js-date-fx.shtml#daysbetween

http://www.merlyn.demon.co.uk/js-date7.htm

tonyp12
02-18-2007, 05:33 PM
var d1 = Date.parse("Feb 18, 2006"); // date 1.
var d2 = new Date(); // date 2, in this case todays date.
document.write(Math.floor((d2.getTime() - d1)/86400000))

Philip M
02-18-2007, 06:15 PM
var d1 = Date.parse("Feb 18, 2006"); // date 1.
var d2 = new Date(); // date 2, in this case todays date.
document.write(Math.floor((d2.getTime() - d1)/86400000))

That works fine for the number of days. But Windy wants the number of weekdays.

tonyp12
02-18-2007, 06:43 PM
oh i see, I did not get that the 'weekdays' refers to Monday-Friday only.

So you want to know how many workdays between two dates.
like what they calculate here:
http://kalender-365.de/weekdays.php

windy
02-18-2007, 06:49 PM
A search of Google found;-

http://lawrence.ecorp.net:32993/inet/samples/js-date-fx.shtml#daysbetween

http://www.merlyn.demon.co.uk/js-date7.htm

Philip - thanks for the alternatives, which I didn't find when I googled. The first one looks interesting but is written with several short form syntax instructions that will take me time to unravel.

Windy

tonyp12
02-18-2007, 07:13 PM
Step1:
calculate how many '5days' between two dates.

document.write(Math.floor((d2.getTime() - d1)/86400000/7*5))

Step2: now I have to think of a way to adjust for that start/end date
that is not even weeks.

I get back to you, as I like to show each step on how I think.

liorean
02-18-2007, 11:43 PM
- What is the best way to handle dates/times that specifically ignores daylight savings, time zones etc. I'm not sure if UTC is affected by DST.UTC is what GMT should have been, except for the fact most GMT calculation actually used BST daylight savings time. UTC is unambiguous. GMT should have been unambigous as well, but so many used GMT when really talking about UK time (GMT with BST used for daylight savings time) that it isn't.
- How is javascript and/or its implementations affected by the change in North America to a different daylight savings schedule starting this March 2007?Answer: It depends on so many factors it's silly. The JavaScript time is based on the local computer time. And there's three choices for local computer time:
1. Computer uses UTC for local clock. Applications (including OS handling) handle conversion to local time themselves. Typically Unix systems.
2. Computer uses local time, OS adjusts computer clock for daylight savings time. Typically Windows systems.
3. Computer uses local time, OS doesn't adjust computer clock for daylight savings time. Applications (including OS handling) handle daylight savings themselves. Typically embedded systems.

So, it can go wrong in many different ways depending on which is the case for the local system. Something that should be reliable enough, though, is that if the user keeps both browser and OS reasonably up to date, you can rely on UTC time to be correct. Of course, I've seen many systems where the computer clock is just dead wrong in any case...
- What is the best way to calculate the number of weekdays between two dates? I have been simply looping between the dates with a counter and only incrementing the counter when getDay() != 0 && getDay() !=6. For longer time spreads this could be quite inefficient.1. Calculate days as the integer number of days in the time interval.
2. Calculate overflow as days % 7.
3. Calculate weekdays as 5 * (days - overflow) / 7
4. Find out which day is the last in the time interval. If Sunday, reduce overflow by 2. If Saturday, reduce overflow by 1.
5. If overflow is larger than 0, increase weekdays by overflow.
Ah, that only works if the interval starts with a Monday. Have to rethink that.

windy
02-18-2007, 11:58 PM
Thanks liorean for the thoughtful post

Windy

liorean
02-19-2007, 12:37 AM
Rethinking step 4: Since overflow is never more than 6 days, it cannot contain more than at max 2 non-workdays. But you can't know exactly where in the overflow you have these non-workdays.

So, a revised algorithm:
4. If overflow is 0 skip to point 10.
5. Set lastday to the last day in the time interval (0=Sunday, 6=Saturday)
6. If lastday is 6, decrease overflow by 1 and skip to point 10.
7. Calculate temp as 1 + lastday - overflow.
8. If temp is 0, decrease overflow by 1 and skip to point 10.
9. If temp is below 0, decrease overflow by 2.
10. Increase weekdays by overflow.

tonyp12
02-21-2007, 03:23 AM
I think this works, I tested a couple of dates it looks good.

Step1: I adjust so all dates are even 7 day weeks starting on a Monday so I can use '/7*5'
Step 2 subtract days that was added but not Sat/Sun.

I move first date backwards and the second date forward to get to nearest Monday.
I have not tested moving BOTH backwards, it that worked I could get rid of adjust2 and 4.


var adjust1= new Array(6,0,1,2,3,4,5)
var adjust2= new Array(1,0,6,5,4,3,2)
var adjust3= new Array(5,0,1,2,3,4,5)
var adjust4= new Array(0,0,4,3,2,1,0)

var d1 = new Date(2007,2,5); // start date
var d2 = d1.getTime() - adjust1[d1.getDay()]*86400000

var d3 = new Date(2007,2,28); //end date (but not including)
var d4 = d3.getTime() + adjust2[d3.getDay()]*86400000

document.write(Math.floor(((d4 - d2)/86400000+1)/7*5) -adjust3[d1.getDay()] -adjust4[d3.getDay()])



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum