Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Some questions on dates

    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

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by windy View Post
    - 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....256CC30078F008

  • #4
    New Coder
    Join Date
    Jan 2007
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor View Post
    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.


    Quote Originally Posted by Philip M View Post
    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

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts

  • #6
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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))

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by tonyp12 View Post
    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.

  • #8
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    New Coder
    Join Date
    Jan 2007
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #10
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by windy View Post
    - 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.

    Edit: Ah, that only works if the interval starts with a Monday. Have to rethink that.
    Last edited by liorean; 02-18-2007 at 10:59 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #12
    New Coder
    Join Date
    Jan 2007
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks liorean for the thoughtful post

    Windy

  • #13
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    Last edited by liorean; 02-18-2007 at 11:39 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #14
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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()])
    Last edited by tonyp12; 02-21-2007 at 04:33 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •