Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Get Date Object from text box

    I've got a textbox, the results of which I've turned into a variable:
    Code:
    question1=document.getElementsByName("RequestDate")[0].value)
    all examples of creating a date object say:
    myObject= New Date();

    I need to create a date object out of 'question1' so I can then add 1 day to it.

    How can I do this?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by ElmoWatson View Post
    I've got a textbox, the results of which I've turned into a variable:
    Code:
    question1=document.getElementsByName("RequestDate")[0].value)
    all examples of creating a date object say:
    myObject= New Date();

    I need to create a date object out of 'question1' so I can then add 1 day to it.

    How can I do this?
    You do not tell us the format of the date entered into the textbox.
    And do you need to validate that it is a valid date, not 56th Febtember?

    BTW, it is new Date(), not New Date() as Javascript is case sensitive.

    Quizmaster: New Zealand was mapped out in the 1770s by which British Explorer?
    Contestant: Robinson Crusoe

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    normal format would be:
    6/5/2014
    I realize that (new) - - I typed it off the top of my head....

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by ElmoWatson View Post
    normal format would be:
    6/5/2014
    I realize that (new) - - I typed it off the top of my head....
    Alas. Is 6/5/2014 supposed to be 6th of May or 5th June?
    Do you want to validate the date (but of course UK and US users will intend different dates)?

    Try this:-

    Code:
    <script type = "text/javascript">
    
    function addDays(dt){
    var ds = dt.split("/");
    var startDate = new Date(ds[2], ds[1]-1, ds[0]);   // YYYY/MM/DD - months in Javascript are 0-11
    var daysAhead = 7;  // change to 6 for days inclusive of start date
    startDate.setTime(startDate.getTime()+(daysAhead*24*60*60*1000));
    var futureDate = startDate.getDate()+"/"+(startDate.getMonth()+1)+"/"+startDate.getFullYear();
    futureDate = futureDate.replace(/^(\d{1}\/)/,"0$1").replace(/(\d{2}\/)(\d{1}\/)/,"$10$2");  // add leading zeroes
    alert("End Date = " + futureDate);
    }
    
    addDays("6/5/2014");  // 6th May 2014
    
    </script>
    Last edited by Philip M; 06-03-2014 at 04:53 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    ElmoWatson (06-03-2014)

  • #5
    New Coder
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Now that I realize you're from London, that is a valid question.
    Here in US, it's Month/Day/Year

    This is a local intranet - the JQuery DatePicker is putting the date into the textbox, so at this point in development, I'm not even worried about validation yet.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Just change this line to

    var startDate = new Date(ds[2], ds[0]-1, ds[1]); // YYYY/MM/DD - months in Javascript are 0-11

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    thanks for the effort, but I now get 5 months and 2 days in the future.
    I have (for instance) today: 6/3/2014
    I want 6/4/2014
    I'm getting 10/5/2014

    Here's my code:
    function addDays(dt)
    {
    var ds = dt.split("/");
    //var startDate = new Date(ds[2], ds[1]-1, ds[0]); // YYYY/MM/DD - months in Javascript are 0-11
    var startDate = new Date(ds[2], ds[0]-1, ds[1]);
    var daysAhead = 7; // change to 6 for days inclusive of start date
    startDate.setTime(startDate.getTime()+(daysAhead*24*60*60*1000));
    var futureDate = startDate.getDate()+"/"+(startDate.getMonth()+1)+"/"+startDate.getFullYear();
    futureDate = futureDate.replace(/^(\d{1}\/)/,"0$1").replace(/(\d{2}\/)(\d{1}\/)/,"$10$2"); // add leading zeroes
    alert("End Date = " + futureDate);
    return futureDate;
    }
    var question1=addDays(document.getElementsByName("RequestDate")[0].value);

  • #8
    New Coder
    Join Date
    Jun 2005
    Location
    Blackpool. UK
    Posts
    98
    Thanks
    0
    Thanked 4 Times in 4 Posts
    the JQuery DatePicke
    So why do you need to convert from a text box when you could use the date object directly?
    Chris

    Indifference will be the downfall of mankind, but who cares?

  • #9
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    it's because your futureDate variable is using dd/mm/yyyy format (and your daysAhead is still set to 7). Change daysAhead to 1 and the var futureDate line to this:
    Code:
    var futureDate = (startDate.getMonth()+1)+"/"+startDate.getDate()+"/"+startDate.getFullYear();

  • Users who have thanked xelawho for this post:

    ElmoWatson (06-03-2014)

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Code:
    <script type = "text/javascript">
    
    function addDays(dt){
    var ds = dt.split("/");
    var startDate = new Date(ds[2], ds[0]-1, ds[1]);   // YYYY/MM/DD - months in Javascript are 0-11
    var daysAhead = 7;  // change to 6 for days inclusive of start date
    startDate.setTime(startDate.getTime()+(daysAhead*24*60*60*1000));
    var futureDate = startDate.getMonth()+1 +"/"+startDate.getDate()+"/"+startDate.getFullYear();
    futureDate = futureDate.replace(/^(\d{1}\/)/,"0$1").replace(/(\d{2}\/)(\d{1}\/)/,"$10$2");  // add leading zeroes
    alert("End Date = " + futureDate);
    }
    
    addDays("07/05/2014");   // month, day, year (5th July 2014)
    
    </script>
    Obviously if you only want one day ahead you can simplify this code.

    startDate.setTime(startDate.getTime()+(24*60*60*1000)); // one day ahead
    Last edited by Philip M; 06-03-2014 at 05:53 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #11
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,374
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by Philip M View Post
    Obviously if you only want one day ahead you can simplify this code.

    startDate.setTime(startDate.getTime()+(24*60*60*1000)); // one day ahead
    actually, it would be simpler just to increment the day and skip all the math.


    JS is smart about handling stuff like "feb 30th": new Date(2014, 1, 30); // == Sun Mar 02 2014



    in related news, i just figured out an easy way to find the 90th day in 2014: new Date(2014, 0, 90); which is == to Mon Mar 31 2014, since jan+mar have 31 days to counter feb's 28...
    i've verified that this "trick" works in chrome, firefox and ie8+...
    Last edited by rnd me; 06-03-2014 at 06:19 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by rnd me View Post
    actually, it would be simpler just to increment the day and skip all the math.
    Simpler but no formatting.


    Code:
    <script type = "text/javascript">
    
    function addOneDay(dt){
    var ds = dt.split("/");
    var nextDate = new Date(ds[2], ds[0]-1, Number(ds[1])+1);   // YYYY/MM/DD - months in Javascript are 0-11
    alert("Tomorrow = " + nextDate);
    }
    
    addOneDay("07/31/2014");   // month, day, year (31st July 2014 USA format)
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #13
    New Coder
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for all the help.
    Between XelaWho & Philip, I got it working just fine.


  •  

    Posting Permissions

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