...

View Full Version : Button date insert



duncan121
03-09-2012, 02:12 PM
Hi all, very new to Javascript, been searching the net for a simple script which has a text box with a button beside and when the visitor clicks the button the date is enter in to the text box automaticly.

Easy, I guess if you know how.

Thanks

Philip M
03-09-2012, 02:19 PM
Like so:-


<input type = "button" value = "Get today's date" onclick = "getTheDate()">
<input type = "text" id = "todayDate" readonly">

<script type = "text/javascript">
function getTheDate() {
document.getElementById("todayDate").value = new Date().toDateString();
}
</script>


Quizmaster: Which ancient Greek poet is also the first name of a main character in "The Simpsons"?
Contestant: Bart

duncan121
03-09-2012, 03:16 PM
Fantastic worked perfectly.

Thanks so much.

duncan121
03-09-2012, 04:08 PM
How can i display the date as YYYY-mm-dd?

Thanks again

Philip M
03-09-2012, 05:53 PM
How can i display the date as YYYY-mm-dd?

Thanks again

Better if you ask for what you want at the outset.


<input type = "button" value = "Get today's date" onclick = "getTheDate()">
<input type = "text" id = "todayDate" readonly">

<script type = "text/javascript">
function getTheDate() {
var today = new Date() ;
var yr = today.getFullYear();
var mth = today.getMonth()+1; // months in Javascript are 0-11
if (mth <10) {mth = "0" + mth}
var dt = today.getDate();
if (dt <10) {dt = "0" + dt}
// you can now display the date in any format you require
var theDate = yr + "-" + mth + "-" + dt;
document.getElementById("todayDate").value = theDate;
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum