0

I am trying to pre-populate a text box with today's date, but for some reason, it keeps showing today's date as 5/30/13, which is a month behind. This code should just be pulling the local time so why is it a month behind? Here is my code, can someone please tell me why the current date is off...Please help, :)

<script type="text/javascript"> /*<![CDATA[*/ function my_curr_date() { var currentDate = new Date() var day = currentDate.getDate(); var month = currentDate.getMonth() + 1; var year = currentDate.getFullYear(); var my_date = month+"-"+day+"-"+year; document.getElementById("dateField").value=my_date; } function orderReady(orderTime){ dateToday.setDate(dateToday.getDate()+orderTime); var ready=dateToday.getMonth()+"/" +dateToday.getDate()+"/"+dateToday.getFullYear(); document.getElementById("duedateField").value=due_date; } /*]]>*/ 

<body onload='return my_curr_date();'> 

<p>Today's Date<br /> <input type='text' name='dateField' id='dateField' value='' /><br /> Pick-up Date<br /> <input type='text' name='duedateField' id='duedateField' value='' /></p> 
0

2 Answers 2

2

Date.getMonth:

The value returned by getMonth is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.

Reference.

Sign up to request clarification or add additional context in comments.

1 Comment

Better to reference ECMA-262.
0

HTML Element:

<p>Today's Date<br /> <input id="date" type="text" name="curDate" size="50" /><br /> Pick-up Date<br /> <input type='text' name='duedateField' id='duedateField' value='' /></p> 

Use this javascript:

var month = Date.getMonth(); var date = Date.getDate(); var year = Date.getYear(); var deliverymonth; var deliverydate; var deliveryyear; var message = month + "/" + date + "/" + year; document.getElementById('date').innerHTML=message; if(month == 11 && date >= 29){ deliveryyear = year + 1; deliverymonth = 1; deleverydate = date - 28; }else{ switch(month) { case 0,2,4,6,7,9,11: if(date >= 29){ deliverymonth = month + 1; deliverydate = date - 28; }else{ deliverydate = date + 3; } break; case 3,5,8,10: if(date >= 28){ deliverymonth = month + 1; deliverydate = date - 27; }else{ deliverydate = date + 3; } break; case 1: if(date >= 26){ deliverymonth = month + 1; deliverydate = date - 25; }else{ deliverydate = date + 3; } break; default: break; } var deliverymessage = deliverymonth + "/" + deliverydate + "/" + deliveryyear; document.getElementById('duedateField').innerHTML=deliverymessage; 

4 Comments

Thank you for your quick responses... I am still running into issues where no the field is not populating at all. I am including the total script below for you to see. I am so confused as to why this isn't working. Again, your help has been and is much appreciated.
<script type="text/javascript"> var month = Date.getMonth(); var date = Date.getDate(); var year = Date.getYear(); var message = month + "/" + date + "/" + year; document.getElementById('date').innerHTML=message; function loadDate(){ var today=dateToday.getMonth() + "/" +dateToday.getDate()+"/"+dateToday.getFullYear(); document.forms[0].curDate.value=today; } function orderReady(orderTime){ dateToday.setDate(dateToday.getDate()+orderTime); var ready=dateToday.getMonth()+"/" +dateToday.getDate()+"/"+dateToday.getFullYear(); document.forms[0].puDate.value=ready; } /*]]>*/ </script>
Ok, so I got the date to prepopulate correctly, but now the picker won't work. Basically, I have to pre-populate the 1st text box with the current date, then populate the 2nd text box with the date + 1-3 days based on the radio box chosen. I have updated my code with the full Javascript code so you can see what it is I'm doing.
see my edit for added content

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.