dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: how to get one html date field to change another date field.

  1. #1
    Join Date
    Mar 2005
    Location
    planet earth
    Posts
    122

    Lightbulb how to get one html date field to change another date field.

    this is how you get one html date field to change another date field. in this case to a date 8 weeks in the future (+4838400 seconds).

    calling html:


    <input name="fieldname" type="date" "onChange="chdat();" />

    javascript function:

    function chdat() {
    var cdate=document.formname.fieldname.value;
    var ddate=Math.round(new Date(cdate).getTime()/1000)+4838400;
    var tdate=new Date(ddate*1000);
    yy=tdate.getFullYear();
    mm=tdate.getMonth()+1; if (mm < 10) mm="0"+mm;
    dd=tdate.getDate(); if (dd < 10) dd ="0"+dd;
    tdatef=yy+'-'+mm+'-'+dd;
    document.formname.fieldname2.value=tdatef;
    }
    Last edited by hitecbill; 12-03-2017 at 09:24 PM.
    captain, i've spotted an M-class planet!

    http://eupeople.net/forum

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,536
    1. When posting, please use forum BBCode tags, examples in my signature.
    2. Date manipulation is easier with the methods supplied

    HTML Code:
    <input name="fieldname" type="date" onchange="chdat( this );" />
    <input name="fieldname2" type="date" />
    </form>
    Code:
    function chdat( fld ) {
    var cdate=fld.value;
    var ddate=new Date(cdate);
    var tdate= new Date(); // make a date object, its date is irrelevant
    // use methods to set the future date based on current date
    tdate.setDate( ddate.getDate() + 1); 
    document.forms.formname.fieldname2.value=tdate.getFullYear()+"-"+pad(tdate.getMonth()+1)+"-"+pad(tdate.getDate());
    }
    function pad( n ){
    return ("00"+n).slice(-2); 
    }
    The value for increment you use is not rught, it equates to adding about 80 minutes... if its such a short time you want to add, then you need to consider using time fields coupled with an initla date field for the date and then a time field for whatever time you input that day it gets whatever time you want added.

    There are other Date methods that you can use.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    573
    4838400 is indeed the number of seconds in 8 weeks but if you are going to manipulate dates "by hand", you need the number of milliseconds. Like \\.\ said, "it equates to adding about 80 minutes".

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center