list of days between two days

Discussion in 'Javascript' started by kirke, Oct 19, 2006.

  1. kirke

    kirke Guest

    Hi. I want to make a list of days between two days.
    How can I make it???
    Thx.
    kirke, Oct 19, 2006
    #1
    1. Advertising

  2. kirke

    RobG Guest

    kirke wrote:
    > Hi. I want to make a list of days between two days.
    > How can I make it???
    > Thx.


    Try:

    <script type="text/javascript">
    function showDays(f){
    var selS = f.startDay;
    var selE = f.endDay;
    var days = [];
    var i = 0;
    while (i<7 && !selS.options.selected){
    i++;
    }
    while (!selE.options.selected){
    days.push(selE.options[i++].text);
    i = i%7;
    }
    days.push(selE.options.text);
    f.dayList.value = days.join('\n');
    }
    </script>
    <form action="">
    <div>
    <label for="startDay">Start day:
    <select name="startDay">
    <option value="0" selected>Monday
    <option value="1">Tuesday
    <option value="2">Wednesday
    <option value="3">Thursday
    <option value="4">Friday
    <option value="5">Saturday
    <option value="6">Sunday
    </select></label>
    <label for="endDay">End day:
    <select name="endDay">
    <option value="0">Monday
    <option value="1">Tuesday
    <option value="2">Wednesday
    <option value="3">Thursday
    <option value="4">Friday
    <option value="5">Saturday
    <option value="6" selected>Sunday
    </select></label>
    <input type="button" value="Show day list"
    onclick="showDays(this.form);">
    <br>
    <textarea readonly name="dayList" cols=10
    rows=8></textarea>
    </div>
    </form>


    --
    Rob
    RobG, Oct 19, 2006
    #2
    1. Advertising

  3. kirke

    kirke Guest

    Wow. yours are great.
    However, I took a mistake. I mean date not day.SORRY.
    Thus, start date is 10/19/2006 and end date is 10/31/2006 Then.
    I want to make a list of 10/19/2006, 10/20/2006,....,10/30/2006,
    10/31/2006 like this.


    RobG wrote:
    > kirke wrote:
    > > Hi. I want to make a list of days between two days.
    > > How can I make it???
    > > Thx.

    >
    > Try:
    >
    > <script type="text/javascript">
    > function showDays(f){
    > var selS = f.startDay;
    > var selE = f.endDay;
    > var days = [];
    > var i = 0;
    > while (i<7 && !selS.options.selected){
    > i++;
    > }
    > while (!selE.options.selected){
    > days.push(selE.options[i++].text);
    > i = i%7;
    > }
    > days.push(selE.options.text);
    > f.dayList.value = days.join('\n');
    > }
    > </script>
    > <form action="">
    > <div>
    > <label for="startDay">Start day:
    > <select name="startDay">
    > <option value="0" selected>Monday
    > <option value="1">Tuesday
    > <option value="2">Wednesday
    > <option value="3">Thursday
    > <option value="4">Friday
    > <option value="5">Saturday
    > <option value="6">Sunday
    > </select></label>
    > <label for="endDay">End day:
    > <select name="endDay">
    > <option value="0">Monday
    > <option value="1">Tuesday
    > <option value="2">Wednesday
    > <option value="3">Thursday
    > <option value="4">Friday
    > <option value="5">Saturday
    > <option value="6" selected>Sunday
    > </select></label>
    > <input type="button" value="Show day list"
    > onclick="showDays(this.form);">
    > <br>
    > <textarea readonly name="dayList" cols=10
    > rows=8></textarea>
    > </div>
    > </form>
    >
    >
    > --
    > Rob
    kirke, Oct 19, 2006
    #3
  4. kirke wrote:

    > Wow. yours are great.
    > However, I took a mistake. I mean date not day.SORRY.
    > Thus, start date is 10/19/2006 and end date is 10/31/2006 Then.
    > I want to make a list of 10/19/2006, 10/20/2006,....,10/30/2006,
    > 10/31/2006 like this.


    Try the Date object.

    I.e.

    var d = new Date(2006,9,19);
    var d2 = new Date(2006,10,31);
    var aDates = [];

    do
    {
    aDates.push(d.toString());
    d.setDate(d.getDate()+1);
    }
    while (d <= d2);

    alert(aDates.join("\r\n"));

    Regards

    Julian
    Julian Turner, Oct 19, 2006
    #4
  5. kirke

    RobG Guest

    kirke wrote:
    > Wow. yours are great.


    Please do not top post here, reply below trimmed quotes of what you are
    replying to.

    > However, I took a mistake. I mean date not day.SORRY.
    > Thus, start date is 10/19/2006 and end date is 10/31/2006 Then.
    > I want to make a list of 10/19/2006, 10/20/2006,....,10/30/2006,
    > 10/31/2006 like this.


    And your attempt at this looks like... ? The date format you have
    shown will likely be misunderstood by the vast majority of web users,
    it is peculiar to only one country that I know of.

    As a hint, get a date from some input elements. Validate the format
    and that they generate a valid date, then use them to create new date
    objects. Add one to the start date until you reach the end date,
    collecting the dates along the way. Then write the list of dates
    you've generated to some element - maybe a text area, span or p
    element.

    The script I posted gives you a start, but you will have to make
    significant changes.

    There are some date routines here:

    <URL: http://www.merlyn.demon.co.uk/js-dates.htm >


    --
    Rob
    RobG, Oct 19, 2006
    #5
  6. kirke

    Evertjan. Guest

    Julian Turner wrote on 19 okt 2006 in comp.lang.javascript:

    >
    > kirke wrote:
    >
    >> Wow. yours are great.
    >> However, I took a mistake. I mean date not day.SORRY.
    >> Thus, start date is 10/19/2006 and end date is 10/31/2006 Then.
    >> I want to make a list of 10/19/2006, 10/20/2006,....,10/30/2006,
    >> 10/31/2006 like this.

    >
    > Try the Date object.
    >
    > I.e.
    >
    > var d = new Date(2006,9,19);
    > var d2 = new Date(2006,10,31);
    > var aDates = [];
    >
    > do
    > {
    > aDates.push(d.toString());
    > d.setDate(d.getDate()+1);
    >}
    > while (d <= d2);
    >
    > alert(aDates.join("\r\n"));


    Good plan.
    To keep up with the OQ, without using the localized version:

    <script type='text/javascript'>

    var d = new Date(2006,9,19);
    var d2 = new Date(2006,10,31);

    while (d <= d2) {
    document.write(d.getYear() +
    '/' + T(d.getMonth()+1) +
    '/' + T(d.getDate()) + '<br>');
    d.setDate(1 + d.getDate());
    };

    function T(x){
    return (x < 10) ? "0" + x : x;
    };

    </script>


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Oct 19, 2006
    #6
  7. kirke

    J R Stockton Guest

    In message <Xns986174E42E238eejj99@194.109.133.242>, Thu, 19 Oct 2006
    09:29:26, Evertjan. <> writes
    >To keep up with the OQ, without using the localized version:
    >
    ><script type='text/javascript'>
    >
    >var d = new Date(2006,9,19);



    var d = new Date("2006/10/19");

    is localised internationally, albeit with a non-ISO separator; it avoids
    that irritating error of 1 in the month, which is liable to lead to
    human error.


    As well as the OP not having defined "day", he or she did not indicate
    whether "between" means "between" or is intended to be inclusive.
    Selection of while-do or do-while loop and of terminal condition should
    fix that.

    If the dates are supplied as Y M D numbers, rather than in an existing
    Object, working in UTC would be faster.

    If the dates are supplied as Objects, one must consider whether they may
    have non-zero time components, and whether that matters.
    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6 ©
    <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.
    J R Stockton, Oct 19, 2006
    #7
  8. J R Stockton wrote:

    [snip]

    > As well as the OP not having defined "day", he or she did not indicate
    > whether "between" means "between" or is intended to be inclusive.
    > Selection of while-do or do-while loop and of terminal condition should
    > fix that.


    [snip]


    The OP gave an example (in a second message):

    "Thus, start date is 10/19/2006 and end date is 10/31/2006 Then.
    I want to make a list of 10/19/2006, 10/20/2006,....,10/30/2006,
    10/31/2006 like this. "

    Regards

    Julian Turner
    Julian Turner, Oct 20, 2006
    #8
    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. Brian Henry
    Replies:
    6
    Views:
    16,154
    Brian Henry
    Nov 19, 2003
  2. gops
    Replies:
    1
    Views:
    661
    Richard Heathfield
    Feb 10, 2004
  3. Replies:
    5
    Views:
    455
    Bo Yang
    Nov 2, 2006
  4. clintonb
    Replies:
    9
    Views:
    852
    James Kanze
    May 23, 2007
  5. W. eWatson
    Replies:
    4
    Views:
    303
    Ari Makela
    Sep 1, 2008
Loading...

Share This Page