Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-09-2012, 01:12 PM   PM User | #1
duncan121
New to the CF scene

 
Join Date: Mar 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
duncan121 is an unknown quantity at this point
Button date insert

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
duncan121 is offline   Reply With Quote
Old 03-09-2012, 01:19 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Like so:-

Code:
<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
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 03-09-2012, 02:16 PM   PM User | #3
duncan121
New to the CF scene

 
Join Date: Mar 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
duncan121 is an unknown quantity at this point
Fantastic worked perfectly.

Thanks so much.
duncan121 is offline   Reply With Quote
Old 03-09-2012, 03:08 PM   PM User | #4
duncan121
New to the CF scene

 
Join Date: Mar 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
duncan121 is an unknown quantity at this point
How can i display the date as YYYY-mm-dd?

Thanks again
duncan121 is offline   Reply With Quote
Old 03-09-2012, 04:53 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by duncan121 View Post
How can i display the date as YYYY-mm-dd?

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

Code:
<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>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:25 AM.


Advertisement
Log in to turn off these ads.