adding dates to form fields

Discussion in 'Javascript' started by gorio, May 15, 2006.

  1. gorio

    gorio Guest

    I have a MySQL/PHP based page with a number of date fields that I would
    like the user to be able to run a script on to update the fields and
    then submit them for server processing.

    All my programming to date has been server side, but I feel this
    exercise is better done client side, hence my post on this group.
    However my javascript knowledge is very limited.

    So far I have something like this, I know it won't work like this but
    I've googled out many variations without success. I'm just presenting
    it in this very basic script to show what I'm trying to achieve.

    function datecalc(form) {
    form.date1.value = form.date0.value+12;
    form.date2.value = form.date1.value+3;
    form.date3.value = form.date2.value+5;
    form.date4.value = form.date3.value+7;
    form.date5.value = form.date4.value+1
    }

    i.e. I just want to add 12 days to date0 to give date1, 3 days to date1
    to give date2 etc etc.

    I feel this should be trivial, but have spent many hours trying to find
    the solution without success. Seems to me simply a matter of getting
    the syntax right but nothing I've tried seems to work, not even an
    error message which makes it that much more difficult.

    I know my form & php & basic java function work, because if I make e.g.
    the first line simply form.date1.value = form.date0.value then when I
    hit the submit button the form value of date1 changes to the value of
    date0.

    The dates in the form are in yyyy-mm-dd format.

    Can someone help with my impending alopecia and show me the way?
     
    gorio, May 15, 2006
    #1
    1. Advertising

  2. gorio

    Evertjan. Guest

    gorio wrote on 15 mei 2006 in comp.lang.javascript:

    > I have a MySQL/PHP based page with a number of date fields that I would
    > like the user to be able to run a script on to update the fields and
    > then submit them for server processing.
    >
    > All my programming to date has been server side, but I feel this
    > exercise is better done client side, hence my post on this group.
    > However my javascript knowledge is very limited.
    >
    > So far I have something like this, I know it won't work like this but
    > I've googled out many variations without success. I'm just presenting
    > it in this very basic script to show what I'm trying to achieve.
    >
    > function datecalc(form) {
    > form.date1.value = form.date0.value+12;
    > form.date2.value = form.date1.value+3;
    > form.date3.value = form.date2.value+5;
    > form.date4.value = form.date3.value+7;
    > form.date5.value = form.date4.value+1
    >}
    >
    > i.e. I just want to add 12 days to date0 to give date1, 3 days to date1
    > to give date2 etc etc.


    the field valuse are strings, so you are just concatenating a number at
    the end of a string.

    >
    > I feel this should be trivial, but have spent many hours trying to find
    > the solution without success. Seems to me simply a matter of getting
    > the syntax right but nothing I've tried seems to work, not even an
    > error message which makes it that much more difficult.


    You will first have to convert the string value to a date object.

    > I know my form & php & basic java function work,


    This is neither php or java.

    Java has nothing to do with javascript but the name.

    > because if I make e.g.
    > the first line simply form.date1.value = form.date0.value then when I
    > hit the submit button the form value of date1 changes to the value of
    > date0.
    >
    > The dates in the form are in yyyy-mm-dd format.
    >
    > Can someone help with my impending alopecia and show me the way?


    ===============

    function stringDateAdd(x,y){ // expects and returns yyyy-(m)m-(d)d
    var d = new Date(x.replace(/-/g,'/'))
    d.setDate(d.getDate()+y)
    return d.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate()
    }

    r = stringDateAdd('2006-5-15',12)
    alert(r)


    r = stringDateAdd('2006-5-30',20)
    alert(r)


    form.date1.value = stringDateAdd(form.date0.value,12)

    ==============

    As alternative you could try the hat-trick ...
    .... for your alopecia



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., May 15, 2006
    #2
    1. Advertising

  3. gorio

    gorio Guest

    thanks very much Evertjan, I tried your code and it works very well and
    thanks for the bonus alert examples.

    One thing which bothers me a bit is the (m)m and (d)d format of the
    days and months which is not in keeping with the yyyy-mm-dd format on
    the rest of my site. Actually it seems MySQL/PHP doesn't care if the
    dates are in yyyy-m-d format since after I update my database the dates
    appear formatted correctly as yyyy-mm-dd, but I want to keep
    consistency and avoid confusing my users.

    I googled and fiddled a bit and found a solution for the months like
    this:

    function stringDateAdd(x,y){ // expects and returns yyyy-(m)m-(d)d
    var d = new Date(x.replace(/-/g,'/'))

    var months=new Array(12);
    months[0]="01"
    months[1]="02"
    months[2]="03"
    months[3]="04"
    months[4]="05"
    months[5]="06"
    months[6]="07"
    months[7]="08"
    months[8]="09"
    months[9]="10"
    months[10]="11"
    months[11]="12"
    var monthnumber = d.getMonth();
    var month_no = months[monthnumber];

    d.setDate(d.getDate()+y)
    return d.getFullYear()+'-'+month_no+'-'+d.getDate()

    }

    Is this a reasonable approach? I would have to do the same for the
    days this way, but from my research javascript just dishes up the d(d)
    m(m) format.
     
    gorio, May 16, 2006
    #3
  4. gorio

    gorio Guest

    thanks very much Evertjan, I tried your code and it works very well and
    thanks for the bonus alert examples.

    One thing which bothers me a bit is the (m)m and (d)d format of the
    days and months which is not in keeping with the yyyy-mm-dd format on
    the rest of my site. Actually it seems MySQL/PHP doesn't care if the
    dates are in yyyy-m-d format since after I update my database the dates
    appear formatted correctly as yyyy-mm-dd, but I want to keep
    consistency and avoid confusing my users.

    I googled and fiddled a bit and found a solution like this:

    function stringDateAdd(x,y){ // expects and returns yyyy-(m)m-(d)d
    var d = new Date(x.replace(/-/g,'/'))

    d.setDate(d.getDate()+y)

    var day_no = (d.getDate())
    if (day_no < 10) { day_no = "0" + day_no; }

    var month_no = (d.getMonth()+1)
    if (month_no < 10) { month_no = "0" + month_no; }

    return d.getFullYear()+'-'+month_no+'-'+day_no

    }

    Is this a reasonable approach? I would have to do the same for the
    days this way, but from my research javascript just dishes up the d(d)
    m(m) format.
     
    gorio, May 16, 2006
    #4
  5. gorio

    gorio Guest

    hmmm... please ignore the code above, I tried it and found it didn't
    work (and in fact didn't think I had posted it at all but somehow my
    browser cached it?)

    anyhow take two on the date formatting:

    I googled and fiddled a bit and found a solution like this:

    function stringDateAdd(x,y){ // expects and returns yyyy-(m)m-(d)d
    var d = new Date(x.replace(/-/g,'/'))

    d.setDate(d.getDate()+y)

    var day_no = (d.getDate())
    if (day_no < 10) { day_no = "0" + day_no; }

    var month_no = (d.getMonth()+1)
    if (month_no < 10) { month_no = "0" + month_no; }

    return d.getFullYear()+'-'+month_no+'-'+day_no

    }

    Is this a reasonable approach? I would have to do the same for the
    days this way, but from my research javascript just dishes up the d(d)
    m(m) format.
     
    gorio, May 16, 2006
    #5
  6. gorio

    gorio Guest

    something screwy going on with my browser here, I didn't mean to post
    the 1st reply at all, and in the 2nd reply the only correct part is the
    updated code. Must try flushing my cache before posting this.
     
    gorio, May 16, 2006
    #6
  7. gorio

    Evertjan. Guest

    gorio wrote on 16 mei 2006 in comp.lang.javascript:

    > something screwy going on with my browser here, I didn't mean to post
    > the 1st reply at all, and in the 2nd reply the only correct part is the
    > updated code. Must try flushing my cache before posting this.
    >


    Worse is that you do not quote.
    This is usenet, not email.
    Others could like to understand too.

    Please quote what you are replying to. If you want to post a followup via
    groups.google.com, don't use the "Reply" link at the bottom of the
    article. Click on "show options" at the top of the article, then click on
    the "Reply" at the bottom of the article headers.
    <http://www.safalra.com/special/googlegroupsreply/>

    =============================

    > I googled and fiddled a bit and found a solution like this:


    This is dangerous, unless you understand the code comleately and
    extensive testing has been done. But it should work.

    > function stringDateAdd(x,y){ // expects and returns yyyy-(m)m-(d)d
    > var d = new Date(x.replace(/-/g,'/'))
    >
    > d.setDate(d.getDate()+y)
    >
    > var day_no = (d.getDate())
    > if (day_no < 10) { day_no = "0" + day_no; }
    >
    > var month_no = (d.getMonth()+1)
    > if (month_no < 10) { month_no = "0" + month_no; }
    >
    > return d.getFullYear()+'-'+month_no+'-'+day_no
    >
    >}


    Exterializing repeated action:

    ========================

    function two(x){
    return (x<10) ? '0'+x : ''+x ;
    };

    // expects yyyy-(m)m-(d)d or yyyy-mm-dd and the number of days to add
    // returns yyyy-mm-dd

    function stringDateAdd(x,y){
    var d = new Date(x.replace(/-/g,'/'));
    d.setDate(d.getDate()+y);
    return d.getFullYear() +
    '-' +
    two(d.getMonth()+1) +
    '-' +
    two(d.getDate());
    };

    ========================

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., May 16, 2006
    #7
  8. JRS: In article <>,
    dated Tue, 16 May 2006 01:05:48 remote, seen in
    news:comp.lang.javascript, gorio <> posted :
    >
    >I googled and fiddled a bit and found a solution for the months like
    >this:


    You should have Googled for the regularly-posted (except currently) and
    (still) frequently-cited newsgroup FAQ, which would have given you all
    that you need to know, including how to post.

    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, May 16, 2006
    #8
  9. gorio

    gorio Guest

    Dr John Stockton wrote:
    >
    > You should have Googled for the regularly-posted (except currently) and
    > (still) frequently-cited newsgroup FAQ, which would have given you all
    > that you need to know, including how to post.
    >

    gee thanks doc, your reply reminds me of the hitchhiker's guide the
    galaxy where the earthlings are admonished for their failure to read
    the intergalactic bulletin regarding the impending demolition of earth,
    but I'll remember to Google that one next time...

    Google search result:
    Your search - regularly-posted (except currently) and (still)
    frequently-cited newsgroup faq javascript - did not match any documents.
     
    gorio, May 17, 2006
    #9
  10. gorio

    Lee Guest

    gorio said:
    >
    >Dr John Stockton wrote:
    >>
    >> You should have Googled for the regularly-posted (except currently) and
    >> (still) frequently-cited newsgroup FAQ, which would have given you all
    >> that you need to know, including how to post.
    >>

    >gee thanks doc, your reply reminds me of the hitchhiker's guide the
    >galaxy where the earthlings are admonished for their failure to read
    >the intergalactic bulletin regarding the impending demolition of earth,
    >but I'll remember to Google that one next time...
    >
    >Google search result:
    >Your search - regularly-posted (except currently) and (still)
    >frequently-cited newsgroup faq javascript - did not match any documents.



    Googling for "comp.lang.javascript faq" finds it immediately.
    Whoever gave you access to newsgroups should have asked you to
    read some basic instructions, including that you should always
    look for a FAQ before posting questions to a newsgroup.


    --
     
    Lee, May 17, 2006
    #10
  11. gorio

    Randy Webb Guest

    Lee said the following on 5/17/2006 10:59 AM:
    > gorio said:
    >> Dr John Stockton wrote:
    >>> You should have Googled for the regularly-posted (except currently) and
    >>> (still) frequently-cited newsgroup FAQ, which would have given you all
    >>> that you need to know, including how to post.
    >>>

    >> gee thanks doc, your reply reminds me of the hitchhiker's guide the
    >> galaxy where the earthlings are admonished for their failure to read
    >> the intergalactic bulletin regarding the impending demolition of earth,
    >> but I'll remember to Google that one next time...
    >>
    >> Google search result:
    >> Your search - regularly-posted (except currently) and (still)
    >> frequently-cited newsgroup faq javascript - did not match any documents.

    >
    >
    > Googling for "comp.lang.javascript faq" finds it immediately.


    Googling for "javascript faq" it is the number 5 hit as well.

    > Whoever gave you access to newsgroups should have asked you to
    > read some basic instructions, including that you should always
    > look for a FAQ before posting questions to a newsgroup.


    Google telling someone to read an FAQ? Surely you jest......

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, May 17, 2006
    #11
  12. gorio wrote:

    > Google search result:
    > Your search - regularly-posted (except currently) and (still)
    > frequently-cited newsgroup faq javascript - did not match any documents.


    "javascript faq", fifth hit (on the first page, with the Google defaults):

    | comp.lang.javascript FAQ - 8.0 - 2004-03-15 - [ ... ]
    | You are reading the comp.lang.javascript meta-FAQ, version 8.0 it is
    | available on the web at ... This is the official comp.lang.javascript
    | (clj) FAQ. ...
    | jibbering.com/faq/ - 62k - [...]


    PointedEars
    --
    There are two possibilities: Either we are alone in the
    universe or we are not. Both are equally terrifying.
    -- Arthur C. Clarke
     
    Thomas 'PointedEars' Lahn, May 22, 2006
    #12
  13. gorio wrote:

    > something screwy going on with my browser here, I didn't mean to post
    > the 1st reply at all, and in the 2nd reply the only correct part is the
    > updated code. Must try flushing my cache before posting this.


    You should not use a Web browser for accessing Network News at all.
    Use a newsreader.


    PointedEars
     
    Thomas 'PointedEars' Lahn, May 22, 2006
    #13
  14. gorio

    Randy Webb Guest

    Thomas 'PointedEars' Lahn said the following on 5/22/2006 12:29 PM:
    > gorio wrote:
    >
    >> something screwy going on with my browser here, I didn't mean to post
    >> the 1st reply at all, and in the 2nd reply the only correct part is the
    >> updated code. Must try flushing my cache before posting this.

    >
    > You should not use a Web browser for accessing Network News at all.
    > Use a newsreader.


    People can use whatever they want for reading Usenet, as long as they
    understand how to use it.

    This is not de.comp.lang.javascript my boy.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, May 22, 2006
    #14
  15. gorio

    Evertjan. Guest

    Re: complex cicles

    padew wrote on 24 aug 2006 in comp.lang.javascript:

    > Array so: for every value 1st column correspond one in 2nd
    >


    Please when starting a new thread, do not include header references to an
    older one. Please try to learn how usenet works.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Aug 24, 2006
    #15
    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:
    906
    Rob Schieber
    Sep 30, 2005
  2. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    233
    Mark Schupp
    Aug 9, 2004
  3. Replies:
    1
    Views:
    238
    Jano Svitok
    Jul 17, 2007
  4. kellygreer1

    RFC-822 dates into Ruby dates

    kellygreer1, Jun 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    261
    Eric I.
    Jun 8, 2008
  5. Dr J R Stockton

    FAQ Dates; Opera Dates.

    Dr J R Stockton, Jun 18, 2007, in forum: Javascript
    Replies:
    0
    Views:
    190
    Dr J R Stockton
    Jun 18, 2007
Loading...

Share This Page