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. Advertisements

  2. gorio

    Evertjan. Guest

    gorio wrote on 15 mei 2006 in comp.lang.javascript:
    the field valuse are strings, so you are just concatenating a number at
    the end of a string.
    You will first have to convert the string value to a date object.
    This is neither php or java.

    Java has nothing to do with javascript but the name.
    ===============

    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., May 15, 2006
    #2
    1. Advertisements

  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:
    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/>

    =============================
    This is dangerous, unless you understand the code comleately and
    extensive testing has been done. But it should work.
    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., May 16, 2006
    #7
  8. JRS: In article <>,
    dated Tue, 16 May 2006 01:05:48 remote, seen in
    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.
     
    Dr John Stockton, May 16, 2006
    #8
  9. gorio

    gorio Guest

    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:

    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:
    Googling for "javascript faq" it is the number 5 hit as well.
    Google telling someone to read an FAQ? Surely you jest......
     
    Randy Webb, May 17, 2006
    #11
  12. "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
     
    Thomas 'PointedEars' Lahn, May 22, 2006
    #12
  13. 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:
    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 Webb, May 22, 2006
    #14
  15. gorio

    Evertjan. Guest

    padew wrote on 24 aug 2006 in comp.lang.javascript:
    Please when starting a new thread, do not include header references to an
    older one. Please try to learn how usenet works.
     
    Evertjan., Aug 24, 2006
    #15
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.