the penultimate week and last week of data for each month

Discussion in 'Javascript' started by SimonC, Dec 31, 2004.

  1. SimonC

    SimonC Guest

    I would like to return data from the last 2 weeks of each given month
    in Javascript, but in 2 formats.

    So, the penultimate week (Monday to Sunday) and the last week (Monday
    to ??)

    I'm not sure if it can be done, but all help welcomed.

    E.g. I have December and would like to see the last 2 weeks.. So this
    doesnt mean the last 15 days. What i mean by this is...

    Sometimes a Week will cross over into 2 months, where it finishes on
    Tuesday and starts on Wednesday, like November 2004 for example.

    With the November 2004 example, means there are 5 weeks in November..
    so i would like to see the data within the 4th week (Monday to Sunday)
    and the 5th week (Monday to Tuesday). Of course for every month of the
    year.
    Is this possible??

    I look forward to hearing your thoughts.
    SimonC
     
    SimonC, Dec 31, 2004
    #1
    1. Advertising

  2. SimonC

    McKirahan Guest

    "SimonC" <> wrote in message
    news:...
    > I would like to return data from the last 2 weeks of each given month
    > in Javascript, but in 2 formats.
    >
    > So, the penultimate week (Monday to Sunday) and the last week (Monday
    > to ??)
    >
    > I'm not sure if it can be done, but all help welcomed.
    >
    > E.g. I have December and would like to see the last 2 weeks.. So this
    > doesnt mean the last 15 days. What i mean by this is...
    >
    > Sometimes a Week will cross over into 2 months, where it finishes on
    > Tuesday and starts on Wednesday, like November 2004 for example.
    >
    > With the November 2004 example, means there are 5 weeks in November..
    > so i would like to see the data within the 4th week (Monday to Sunday)
    > and the 5th week (Monday to Tuesday). Of course for every month of the
    > year.
    > Is this possible??
    >
    > I look forward to hearing your thoughts.
    > SimonC
    >


    Will this help? Watch for word-wrap.

    Option Explicit
    '*
    '* Declare Constants
    '*
    Const cVBS = "penuweek.vbs"
    Const cYER = 2004
    '*
    '* Declare Variables
    '*
    Dim arrDAT(12,1)
    Dim intDAT
    Dim strDAT
    Dim intDOW
    Dim intMON
    Dim strYER
    strYER = cYER
    '*
    '* 12 Months
    '*
    For intMON = 1 To 12
    '*
    '* First day of next month
    '*
    strDAT = DateSerial(strYER,intMON+1,1)
    '*
    '* Last day of this month
    '*
    strDAT = DateAdd("d",-1,strDAT)
    '*
    '* Day of week
    '*
    intDOW = DatePart("w",strDAT,2)
    '*
    '* Date of Monday before last
    '*
    If intDOW = 1 Then intDOW = 8
    intDAT = (intDOW + 6) * -1
    strDAT = DateAdd("d",intDAT,strDAT)
    arrDAT(intMON,0) = strDAT
    '*
    '* Date of Sunday two weeks later
    '*
    strDAT = DateAdd("d",13,strDAT)
    arrDAT(intMON,1) = strDAT
    Next
    '*
    '* Results
    '*
    strDAT = ""
    For intMON = 1 To 12
    strDAT = strDAT & intMON & ". "
    strDAT = strDAT & arrDAT(intMON,0) & " : "
    strDAT = strDAT & arrDAT(intMON,1) & vbCrLf
    Next
    MsgBox strDAT,vbInformation,cVBS
     
    McKirahan, Dec 31, 2004
    #2
    1. Advertising

  3. SimonC

    Zifud Guest

    McKirahan wrote:
    > "SimonC" <> wrote in message
    > news:...
    >
    >>I would like to return data from the last 2 weeks of each given month
    >>in Javascript, but in 2 formats.

    [...]
    >
    > Will this help? Watch for word-wrap.
    >


    Since this is a JavaScript forum, and the OP has explicitly asked for
    JavaScript, why would 'this' help?

    > Option Explicit
    > '*
    > '* Declare Constants

    [...]

    Perhaps if you re-write it in an appropriate language...

    --
    Zif
     
    Zifud, Dec 31, 2004
    #3
  4. SimonC

    McKirahan Guest

    "Zifud" <> wrote in message
    news:41d55f2a$0$2572$...
    > McKirahan wrote:
    > > "SimonC" <> wrote in message
    > > news:...
    > >
    > >>I would like to return data from the last 2 weeks of each given month
    > >>in Javascript, but in 2 formats.

    > [...]
    > >
    > > Will this help? Watch for word-wrap.
    > >

    >
    > Since this is a JavaScript forum, and the OP has explicitly asked for
    > JavaScript, why would 'this' help?
    >
    > > Option Explicit
    > > '*
    > > '* Declare Constants

    > [...]
    >
    > Perhaps if you re-write it in an appropriate language...
    >
    > --
    > Zif


    The OP did ask "Is this possible??" -- "this" (at least) shows that it is...


    You're right, of course. I post to multiple groups and sometimes when I
    think of a solution I forget to customize it to the group.
     
    McKirahan, Dec 31, 2004
    #4
  5. SimonC

    Mick White Guest

    SimonC wrote:
    > I would like to return data from the last 2 weeks of each given month
    > in Javascript, but in 2 formats.
    >
    > So, the penultimate week (Monday to Sunday) and the last week (Monday
    > to ??)
    >
    > I'm not sure if it can be done, but all help welcomed.
    >
    > E.g. I have December and would like to see the last 2 weeks.. So this
    > doesnt mean the last 15 days. What i mean by this is...
    >
    > Sometimes a Week will cross over into 2 months, where it finishes on
    > Tuesday and starts on Wednesday, like November 2004 for example.
    >
    > With the November 2004 example, means there are 5 weeks in November..
    > so i would like to see the data within the 4th week (Monday to Sunday)
    > and the 5th week (Monday to Tuesday). Of course for every month of the
    > year.
    > Is this possible??
    >
    > I look forward to hearing your thoughts.
    > SimonC
    >


    days=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];

    // Find the Nth day(Three character string,
    // e.g "Thu", or use index [Sun=0, Sat=6])
    // of the month (full) of a given year (full)
    // To find the first Sunday of April 2004:
    // var spring = getNthDayInMonth(1,"Sun","April",2004)
    // or spring = getNthDayInMonth(1,0,"April",2004)
    // returns "4" (April 4th)
    function getNthDayInMonth(N,day,month,year){
    day=(getIndex(day,days)>-1)? getIndex(day,days):day;
    b=getMonthLength(month,year);
    d=7*N - 6 + (7+day-getFirstDayOfMonth(month,year))%7
    return d<b? d:"error";
    }
    See http://mickweb.com/javascript/dates/dateFunctions.html for
    supporting functions
    Mick
     
    Mick White, Dec 31, 2004
    #5
  6. JRS: In article <>
    , dated Fri, 31 Dec 2004 03:04:46, seen in news:comp.lang.javascript,
    SimonC <> posted :
    >I would like to return data from the last 2 weeks of each given month
    >in Javascript, but in 2 formats.


    Firstly, beware of American solutions; they have funny ideas about dates
    over there, and theirs may not be what you want.


    FYI, ISO 8601:2000 weeks are ALWAYS Mon=1 to Sun=7. ISO 8601:2000 does
    not define week-of-month (ISO 8601:2004 might, though); but there is an
    obvious definition by analogy with week-of-year; a week belongs to that
    Calendar Month in which its Thursday lies.


    >So, the penultimate week (Monday to Sunday) and the last week (Monday
    >to ??)
    >
    >I'm not sure if it can be done, but all help welcomed.
    >
    >E.g. I have December and would like to see the last 2 weeks.. So this
    >doesnt mean the last 15 days. What i mean by this is...
    >
    >Sometimes a Week will cross over into 2 months, where it finishes on
    >Tuesday and starts on Wednesday, like November 2004 for example.
    >
    >With the November 2004 example, means there are 5 weeks in November..
    >so i would like to see the data within the 4th week (Monday to Sunday)
    >and the 5th week (Monday to Tuesday). Of course for every month of the
    >year.


    If the last week ends on the last day of the month, and that is not
    Sunday, then are you happy about having days in the following month
    before the Monday that starts its first full week?


    >Is this possible??


    Yes, but one must first understand it, and maybe believe it.


    AFAICS, your last week starts on the last Monday of the month, which is
    easily calculated as the zeroth Monday of the following month, or as
    0..6 days before the zeroth of that month; and the previous week starts
    7 days earlier.

    I think the following fits the case; it will show the desired interval
    for the first to the 20th months of 2004. Adapt to suit. Test.

    for (j=1; j<20; j++) {
    X = new Date(2004, j, 0) ; D = new Date(+X) // D is a copy of X
    W = D.getDay() // 0..6 Sun-Sat
    D.setDate(D.getDate() - (W+6)%7 - 7) // back 0..6 + 7 days
    document.writeln(D, ' \t ', X, '<br>')
    }

    *** *** *** Read the newsgroup FAQ; see below.

    --
    © 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, Dec 31, 2004
    #6
  7. JRS: In article <rLbBd.593649$wV.485877@attbi_s54>, dated Fri, 31 Dec
    2004 12:38:47, seen in news:comp.lang.javascript, McKirahan
    <> posted :
    > For intMON = 1 To 12
    > '*
    > '* First day of next month
    > '*
    > strDAT = DateSerial(strYER,intMON+1,1)
    > '*
    > '* Last day of this month
    > '*
    > strDAT = DateAdd("d",-1,strDAT)



    (a) For the second line, why not just strDat = strDat-1 ??

    (b) For both, why not strDAT = DateSerial(strYER, intMON+1, 0) ??


    You should learn more about the languages that you use before you
    presume to be able to write efficient algorithms; and you should note
    that java != VB.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr John Stockton, Dec 31, 2004
    #7
  8. SimonC

    SimonC Guest

    Dr John Stockton wrote:
    > JRS: In article <rLbBd.593649$wV.485877@attbi_s54>, dated Fri, 31

    Dec
    > 2004 12:38:47, seen in news:comp.lang.javascript, McKirahan
    > <> posted :
    > > For intMON = 1 To 12
    > > '*
    > > '* First day of next month
    > > '*
    > > strDAT = DateSerial(strYER,intMON+1,1)
    > > '*
    > > '* Last day of this month
    > > '*
    > > strDAT = DateAdd("d",-1,strDAT)

    >
    >
    > (a) For the second line, why not just strDat = strDat-1 ??
    >
    > (b) For both, why not strDAT = DateSerial(strYER, intMON+1, 0)

    ??
    >
    >
    > You should learn more about the languages that you use before you
    > presume to be able to write efficient algorithms; and you should note
    > that java != VB.
    >
    > --
    > © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00

    MIME. ©
    > Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links,

    acronyms
    > PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see

    00index.htm
    > Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm

    critdate.htm etc.

    --------------------------------------------------------------------
    Thanks guys for all the help. I am starting to understand some of it
    more now. As you're probably aware, im no JavaScript expert... Quite
    the opposite.

    I have tried the scripts posted on here, but they're not quite working
    in the way i would like.

    I am actually trying to produce this data against a report that i have
    written against a Database. The tool is called "Brio" (like Crystal
    reports). So i have my columns of data. Now, the data i would like to
    get is as described above, but against a column name (field) named as
    "NoOfOrders" (this is a date field in the following format: 01/01/2004
    00:00:00)

    Just as an example of what i have already in the report in terms of
    Javascript, below is a simple script i created.
    --------
    DayNo = new Date(NoOfOrders); weekday=DayNo.getDay()
    --------

    Again, all help on this would be great and thanks again for the current
    input.

    Cheers...
    SimonC
     
    SimonC, Jan 3, 2005
    #8
  9. JRS: In article <HBeBd.108072$>, dated
    Fri, 31 Dec 2004 15:53:11, seen in news:comp.lang.javascript, Mick White
    <> posted :

    >days=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
    >
    >// Find the Nth day(Three character string,
    >// e.g "Thu", or use index [Sun=0, Sat=6])
    >// of the month (full) of a given year (full)
    >// To find the first Sunday of April 2004:
    >// var spring = getNthDayInMonth(1,"Sun","April",2004)
    >// or spring = getNthDayInMonth(1,0,"April",2004)
    >// returns "4" (April 4th)
    >function getNthDayInMonth(N,day,month,year){
    >day=(getIndex(day,days)>-1)? getIndex(day,days):day;
    >b=getMonthLength(month,year);
    >d=7*N - 6 + (7+day-getFirstDayOfMonth(month,year))%7
    >return d<b? d:"error";
    >}
    >See http://mickweb.com/javascript/dates/dateFunctions.html for
    >supporting functions



    For any given month, there is a value of N, 4 or 5, that should select
    the very last day; e.g. 2005-01-31 is the Fifth Monday of this month.
    But I would expect your b=getMonthLength(month,year) to be 31 for this
    month, which the code seems to show as giving an error message for d=31.
    Have I misunderstood?

    If a Date Object is to be generated from the result, ISTM easier and
    safer to confirm that getMonth() is as it should be. For speed, you
    could use something like
    return ( d < 29 || d <= getMonthLength ) ? d : 0
    so that the expensive getMonthLength is only called in a few cases.

    In practice, the month-length can only matter for N=5; and, as the
    outside world has spotted this, the 5th X-day is rarely called for,
    except when specified to mean the last X-day. The latter can easily be
    obtained as the zeroth X-day of the following month.

    Your function, as shown, pollutes the namespace by stomping on the
    previous value of globals b & d.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr John Stockton, Jan 3, 2005
    #9
  10. JRS: In article <>,
    dated Mon, 3 Jan 2005 00:56:27, seen in news:comp.lang.javascript,
    SimonC <> posted :

    > Now, the data i would like to
    >get is as described above, but against a column name (field) named as
    >"NoOfOrders" (this is a date field in the following format: 01/01/2004
    >00:00:00)


    Not a good example date. Better to choose a date after the 12th of its
    month; your example is compatible with each of the customary UK & US
    formats. Better still to use, say, 2004/01/17 or 2004-01-17.

    >Just as an example of what i have already in the report in terms of
    >Javascript, below is a simple script i created.
    >--------
    >DayNo = new Date(NoOfOrders); weekday=DayNo.getDay()
    >--------


    If NoOfOrders means anything like Number of Orders, ISTM unlikely
    (unless orders are received at exactly one per millisecond, starting at
    1970-00-01 00:00:00.001 GMT) that it can be so converted to a reasonable
    DayNo. Note that getDay returns 0..6; to get standard ISO/EU/UK day
    numbering, that 0 must become a 7.

    See below.

    --
    © 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 3, 2005
    #10
  11. SimonC

    Mick White Guest

    Dr John Stockton wrote:

    > For any given month, there is a value of N, 4 or 5, that should select
    > the very last day; e.g. 2005-01-31 is the Fifth Monday of this month.
    > But I would expect your b=getMonthLength(month,year) to be 31 for this
    > month, which the code seems to show as giving an error message for d=31.
    > Have I misunderstood?


    Thanks for the observation, I'll revisit the code, now that I have a few
    years of js coding under my belt, I'll optimise the code.

    >
    > If a Date Object is to be generated from the result, ISTM easier and
    > safer to confirm that getMonth() is as it should be. For speed, you
    > could use something like
    > return ( d < 29 || d <= getMonthLength ) ? d : 0
    > so that the expensive getMonthLength is only called in a few cases.


    A good point, since February is the only non-conformer.


    >
    > In practice, the month-length can only matter for N=5; and, as the
    > outside world has spotted this, the 5th X-day is rarely called for,
    > except when specified to mean the last X-day. The latter can easily be
    > obtained as the zeroth X-day of the following month.


    Another timesaver, I hadn't made this logical leap (N can be only 0-4 or 5)


    >
    > Your function, as shown, pollutes the namespace by stomping on the
    > previous value of globals b & d.
    >

    Ugly, isn't it?

    Thanks.
    Mick
     
    Mick White, Jan 4, 2005
    #11
  12. SimonC

    SimonC Guest

    Just to confirm.. NoOfOrders is simple a name, that contains dates of
    that particular order. I have extrated this field out of a query and i
    am using it inside this current one. You're right.. Ugly it is.

    The "NoOfOrders" field is in the format of a Date like this:
    "31/01/2005 00:00:00"

    I assume that the code that is above, i have to call the "NoOfOrders"
    field somewhere within the code to apply it!!?!? Sorry about this, but
    i really dont know much Javascript.

    Cheers...
    SimonC
     
    SimonC, Jan 4, 2005
    #12
  13. JRS: In article <WLlCd.123101$>, dated
    Tue, 4 Jan 2005 00:51:02, seen in news:comp.lang.javascript, Mick White
    <> posted :
    >Dr John Stockton wrote:
    >
    >> For any given month, there is a value of N, 4 or 5, that should select
    >> the very last day; e.g. 2005-01-31 is the Fifth Monday of this month.
    >> But I would expect your b=getMonthLength(month,year) to be 31 for this
    >> month, which the code seems to show as giving an error message for d=31.
    >> Have I misunderstood?

    >
    >Thanks for the observation, I'll revisit the code, now that I have a few
    >years of js coding under my belt, I'll optimise the code.


    Feel free to visit <URL:http://www.merlyn.demon.co.uk/js-date7.htm#NXM>
    and thereabouts.

    --
    © 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 4, 2005
    #13
  14. JRS: In article <>
    , dated Tue, 4 Jan 2005 02:39:38, seen in news:comp.lang.javascript,
    SimonC <> posted :
    >
    >The "NoOfOrders" field is in the format of a Date like this:
    >"31/01/2005 00:00:00"


    Be warned : javascript is liable, even in the UK, to interpret that date
    as being FFF, i.e. MM/DD/YYYY - month 31 day 1 of 2005, 2007-07-01.

    See sig below.

    --
    © 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 4, 2005
    #14
    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. Guest
    Replies:
    3
    Views:
    1,886
    Alexandre
    Dec 22, 2003
  2. Mufasa
    Replies:
    0
    Views:
    418
    Mufasa
    Sep 5, 2008
  3. Guest
    Replies:
    3
    Views:
    548
    Alexandre
    Dec 22, 2003
  4. Guest
    Replies:
    3
    Views:
    1,182
    Alexandre
    Dec 22, 2003
  5. Charlotte
    Replies:
    2
    Views:
    217
Loading...

Share This Page