1

I've got this:

    var lDate = document.getElementById('txtLeaveDate');
    var rDate = document.getElementById('txtReturnedDate');

Err...javascript so how do I assign the value of txtLeaveDate to a date variable

I tried:

var myDate = new Date(lDate.value);

But this assigns some long value....

I can do it if I try:

var today = new Date();
  var day2 = new Date();
  day2.setDate(today.getDate() + 30);

But the issue is I need to get the date from txtLeaveDate not by a date variable

edit complete code

var theLDate = new Date(lDate.value);
        var theRDate = new Date(rDate.value);

        //check if return date is a sunday, if it is no need
        //to do anything,
        //else make it a sunday
        while (theRDate.getDay() != 0) 
            theRDate.setDate(theRDate.getDate() + 1);

        //at this point RDate is a sunday...
        while(theLDate.valueOf() <= theRDate.valueOf())
            {
                if(theLDate.getDay() == 0)
                    {   //sunday
                        var li = document.createElement('li');
                        li.setAttribute('id', ['liID' + count]);
                        var month = theLDate.getMonth();
                        var day = theLDate.getDate();
                        var year = theLDate.getFullYear();
                        var theDay = month + '/' + day + '/' + year + ' (Sunday)';
                        li.innerHTML = theDay;
                        ul.appendChild(li);
                    }   
                theLDate.setDate(theLDate.getDate() + 1);
                count++;
            } 

But when I pick 2 dates in my calendar like so:

enter image description here

oJM86o
  • 2,108
  • 8
  • 43
  • 71

3 Answers3

1

if I try that and say alert(theLDate.valueOf()); it returns 1309924800000

That's because that is the value of a Date object, measured in milliseconds since 1/1/1970 00:00:00, in this case corresponding to Wed Jul 6 04:00:00 2011 UTC.

Try using .toString() instead and you'll see the corresponding date in a human readable format.

The problem with your dates appearing to be in June is because the getMonth() function for odd reasons returns the month zero based, i.e. January == 0.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • This looks like the correct answer to me. In addition, this line of code should be changed from this" `li.setAttribute('id', ['liID' + count]);` to this `li.setAttribute('id', 'liID' + count);` – jfriend00 Jul 25 '11 at 18:44
0

You need to use .innerHTML, otherwise you are not returning the text in the element.

var lDate = document.getElementById('txtLeaveDate').innerHTML;
var myDate = new Date(lDate);
document.write(myDate);

http://jsfiddle.net/jasongennaro/ua85k/

Jason Gennaro
  • 34,535
  • 8
  • 65
  • 86
0

Months returned by the someDate.getMonth method are zero-indexed (from 0 to 11). So if using them to create a string add 1!

var month = theLDate.getMonth() + 1;
James
  • 20,957
  • 5
  • 26
  • 41