PDA

View Full Version : how to show current date in the input text field using variable



meenakshi
05-15-2008, 07:21 PM
hi
i m unable to rectify this problem and m unable to get a solution also
pls help me.
i want to have the current date showing in the input field


var cdat=(new Date());
var newcdatsep1="-";
var newcdat="00" + cdat.getDate();
var newcdat1="00" + (cdat.getMonth()+1);
newcdat=newcdat.substring(newcdat.length - 2);
newcdat1=newcdat1.substring(newcdat1.length - 2);
var newcdat3=newcdat+newcdatsep1+newcdat1;


<td>Date</td>
<td>
<input type="text" value="newcdat3" tabindex="9" size="20">
</td>

here i thought the value should show me the current date but it is not showing anything
what i have to do to achieve this
if someone can help me rectify this i wud be really greatful
thanks
anand

meenakshi
05-15-2008, 08:08 PM
can anyone help pls??????

mburt
05-15-2008, 08:32 PM
Well your date variables look sort of right, but you aren't actually applying it to the input field:


<input type="text" value="newcdat3" tabindex="9" size="20" id="date">
<script type="text/javascript">
$ = function(e) {return document.getElementById(e);};
var x = new Date(),
m = x.getMonth(),
d = x.getDate(),
y = x.getYear();
$("date").value = (m<10?"0"+m:m)+"-"+(d<10?"0"+d:d)+"-"+y;
</script>

I couldn't figure out the date format you were trying to use, so I used the basic month-day-year format.

meenakshi
05-16-2008, 04:05 AM
hi
wow it is actually working
thanks a million
smile always
:)
ur too good
anand