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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Parsing to a date object

    Hello,

    I am trying to pass a variable to a date object and then increment the date object. The format of the variable is dd/mm/yyyy. I have tried using Date.parse but am getting incorrect results:

    The date I am trying parse is:

    15/10/2012
    Code:
    var myDatetemp=Date.parse(document.getElementById("loggeddate").value);
    var myDate=new Date(myDatetemp);
    I then increment the date by, say 1 day:

    Code:
    myDate.setDate(myDate.getDate()+1);document.getElementById('deadline').value=myDate.getDate() + "/" + (myDate.getMonth()+1)+ "/" + myDate.getYear();break;
    What I get returned is:

    11/3/2013

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    The problem is that a string date will generally be interpreted as mm/dd/yyyy. And the 10th day of the 15th month 2012 will be "translated" into the 10th day of the 3rd month 2013. And +1 will be the 11th day then.

    Try this (make sure that day and month are always two digits!)
    Code:
    var mydatestring = document.getElementById("loggeddate").value;
    var myyear = parseInt(mydatestring.substring(6), 10);
    var mymonth = parseInt(mydatestring.substring(3, 5), 10) - 1;
    var myday = parseInt(mydatestring.substring(0, 2), 10);
    var myDate = new Date(myyear, mymonth, myday);
    myDate.setDate(myDate.getDate()+1);
    document.getElementById('deadline').value=myDate.getDate() + "/" + (myDate.getMonth()+1)+ "/" + myDate.getYear();

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Try using this:

    var Date = new Date();
    var DaysToAdd = 6;
    someDate.setDate(Date.getDate() + DaysToAdd);

    Formatting Date to dd/mm/yyyy format:
    var dd = Date.getDate();
    var mm = Date.getMonth() + 1;
    var yyyy = Date.getFullYear();

    var NewDate = dd + '/'+ mm + '/'+ yyyy;

    Hope this helps.

  • #4
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you both for your help.


  •  

    Posting Permissions

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