Calculate the number of WEEKDAYS between 2 dates?

Discussion in 'Javascript' started by SimonC, Jan 25, 2005.

  1. SimonC

    SimonC Guest

    Help needed for a Javascript beginner.

    As above in the subject... i need a javascript to run this, but not in
    the form of a web-page. I want to calculate it between 2 fields in a
    database that i have extracted into a report writer.
    Look forward to hearing..

    Cheers...
    SimonC
    SimonC, Jan 25, 2005
    #1
    1. Advertising

  2. SimonC

    Mick White Guest

    SimonC wrote:
    > Help needed for a Javascript beginner.
    >
    > As above in the subject... i need a javascript to run this, but not in
    > the form of a web-page. I want to calculate it between 2 fields in a
    > database that i have extracted into a report writer.
    > Look forward to hearing..
    >



    Use the mySQL(or whatever) maths and date functions.
    Mick
    Mick White, Jan 25, 2005
    #2
    1. Advertising

  3. SimonC

    McKirahan Guest

    "SimonC" <> wrote in message
    news:...
    > Help needed for a Javascript beginner.
    >
    > As above in the subject... i need a javascript to run this, but not in
    > the form of a web-page. I want to calculate it between 2 fields in a
    > database that i have extracted into a report writer.
    > Look forward to hearing..
    >
    > Cheers...
    > SimonC
    >


    Is VBScript an option? If so, look at "DateDiff()".

    Cut-and-paste the following into an IE Address bar:

    vbscript:DateDiff("d",#01/01/2005#,#01/26/2005#)
    McKirahan, Jan 25, 2005
    #3
  4. JRS: In article <>, dated Tue, 25 Jan 2005
    12:43:05, seen in news:comp.lang.javascript, McKirahan <>
    posted :
    >"SimonC" <> wrote in message
    >news:...


    >> As above in the subject...


    You could have copied it. Do not presume that the Subject line is readily
    visible when the Body is being read. It is:
    Calculate the number of WEEKDAYS between 2 dates?


    > i need a javascript to run this, but not in
    >> the form of a web-page. I want to calculate it between 2 fields in a
    >> database that i have extracted into a report writer.
    >> Look forward to hearing..


    >Is VBScript an option? If so, look at "DateDiff()".
    >
    >Cut-and-paste the following into an IE Address bar:
    >
    >vbscript:DateDiff("d",#01/01/2005#,#01/26/2005#)


    It would help if you were both to read and understand the question
    before answering. Your code gives me 25.

    The OP is posting from the UK; he cannot be expected to want FFF dates.

    The difference in those dates is 25 days.
    There are 24 days between those dates.
    There are 17 weekdays between those dates, or 18 inclusive (Sat..Wed).

    You could have answered with corresponding javascript:
    javascript:Math.round((new Date("2005/01/26")-new Date("2005/01/01"))/864e5)
    also gives 25 from the address bar.

    You do not need DateDiff :
    vbscript:CDate(#01/26/2005#)-CDate(#01/01/2005#)
    and indeed vbscript:#01/26/2005#-#01/01/2005#
    give 25 too.


    The OP can find what he needs in js-date7.htm, via FAQ; see below.
    He will have to consider the meaning of "between".

    Alternatively, the following method will do well enough for short
    intervals (adapt for desired end-effects) :

    D = new Date("2005/01/01")
    E = new Date("2005/01/26").setHours(12)
    N = 0
    while (D<E) {
    if (D.getDay()%6!=0) N++
    D.setDate(D.getDate()+1)
    }
    N


    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Jan 26, 2005
    #4
  5. SimonC

    Fred Oz Guest

    Dr John Stockton wrote:
    [...]
    > The OP can find what he needs in js-date7.htm, via FAQ; see below.


    And the request for "weekdays" suggest business days, which are
    affected by public holidays. Therefore reference to a calendar
    may be required.

    Allowing for international (or even interstate) variances
    suggests some serious issues with attempting to accurately count
    the number of business or working days.

    > Alternatively, the following method will do well enough for short
    > intervals (adapt for desired end-effects) :
    >
    > D = new Date("2005/01/01")
    > E = new Date("2005/01/26").setHours(12)
    > N = 0
    > while (D<E) {
    > if (D.getDay()%6!=0) N++
    > D.setDate(D.getDate()+1)
    > }
    > N


    Neat accommodation of Sunday being day zero. Then again, the
    test would be even simpler if Monday was zero...

    --
    Fred
    Fred Oz, Jan 27, 2005
    #5
  6. JRS: In article <vPXJd.286$>, dated Thu, 27
    Jan 2005 01:52:27, seen in news:comp.lang.javascript, Fred Oz
    <> posted :
    >Dr John Stockton wrote:
    >[...]
    >> The OP can find what he needs in js-date7.htm, via FAQ; see below.

    >
    > And the request for "weekdays" suggest business days, which are
    > affected by public holidays. Therefore reference to a calendar
    > may be required.


    Since the OP was posting from UK, I took him to mean what he wrote.

    > Allowing for international (or even interstate) variances
    > suggests some serious issues with attempting to accurately count
    > the number of business or working days.


    Interstate - what's that? <g>

    But, at least in the UK, there are some things for which Mon-Fri differs
    from Sat,Sun without being affected by holidays. For example, my
    preferred Internet dial-up number currently works all day Sat,Sun but on
    other days not between 08:00 & 18:00 - and that is totally unaffected by
    Christmas Day and other holidays.


    >> if (D.getDay()%6!=0) N++


    > Neat accommodation of Sunday being day zero.


    Available to all who read the newsgroup FAQ with care; as is stuff on
    allowing for holidays.

    > Then again, the
    > test would be even simpler if Monday was zero...


    Or if Sunday were seven, as called for by ISO 8601.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Jan 27, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    843
    Rob Schieber
    Sep 30, 2005
  2. Mike

    calculate weekdays

    Mike, Jul 11, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    348
  3. JP SIngh

    Calculate Weekdays in ASP

    JP SIngh, Jul 13, 2004, in forum: ASP General
    Replies:
    2
    Views:
    193
    Aaron [SQL Server MVP]
    Jul 13, 2004
  4. chrismo
    Replies:
    2
    Views:
    171
    Phrogz
    Jan 16, 2007
  5. Luca Villa
    Replies:
    2
    Views:
    142
    Luca Villa
    Nov 11, 2007
Loading...

Share This Page