Question/Help with JavaScript time/date

add a paragraph with the current time AND date

Page 1 of 1

3 Replies - 686 Views - Last Post: 13 September 2008 - 02:07 PM

#1 skillionaire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 13-September 08

Question/Help with JavaScript time/date

Posted 13 September 2008 - 12:26 PM

Hey all
I got a small question:

I need to build a HTML page that has a form with one submit button. When I click on the button, I need to stop the page from submitting and then add (under the button) a paragraph with the current time and date.

I pretty much everything working here EXCEPT it does not show the time in numbers... it shows the time in words.

Heres the code

	<script>
	function v(){
	
	var today = new Date()
	var myDate = today.getHours() 
	var myTime = today.getMinutes()
	var mySeconds = today.getSeconds()
	
		
		var myParagraph = document.createElement("p")
		
	var txt = document.createTextNode('today');
	myParagraph.appendChild(txt);
	document.getElementById("bob").appendChild(myParagraph);
		
		return false;	
	}
	

	
	</script>


<form name="myform" id="myform" action="javascript3.html" method="post" onsubmit="return v()">
	<div class="formBox">
		<label for="sample">User Info</label>
		<input type="text" name="sample" id="sample" value="" />
	</div>
	<div class="formBox" id="bob">
		<input type="submit" name="btnSubmit" id="btnSubmit" value="Click Me" />
		<p></p>
	</div>
</form>


I just don't know whats wrong.
Any help would mean a lot to me. Been trying (I really have) from 9AM to 1pm -

Is This A Good Question/Topic? 0
  • +

Replies To: Question/Help with JavaScript time/date

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,149
  • Joined: 07-September 06

Re: Question/Help with JavaScript time/date

Posted 13 September 2008 - 12:34 PM

You are setting the textNode's value to 'today', as in the string 'today' not the variable today.

If you just want it to show the date and time here it is:

<script>
function v(){
  var today = new Date();
  var myParagraph = document.createElement("p");
  var txt = document.createTextNode(today);
  myParagraph.appendChild(txt);
  document.getElementById("bob").appendChild(myParagraph);
  return false;	
}
</script>


If you want to have the current time shown then here:

<script>
function v(){
  var today = new Date()
  var myDate = today.getHours() 
  var myTime = today.getMinutes()
  var mySeconds = today.getSeconds()
  var myParagraph = document.createElement("p")
  var txt = document.createTextNode("Current time: "+myDate%12+":"+myTime+"."+mySeconds);
  myParagraph.appendChild(txt);
  document.getElementById("bob").appendChild(myParagraph);
  return false;	
}
</script>

Was This Post Helpful? 1
  • +
  • -

#3 skillionaire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 13-September 08

Re: Question/Help with JavaScript time/date

Posted 13 September 2008 - 12:57 PM

Omg !

Thank you so much - I knew it was a silly little thing that I overlooked. Thank you so much!
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,149
  • Joined: 07-September 06

Re: Question/Help with JavaScript time/date

Posted 13 September 2008 - 02:07 PM

No problem, just glad to have been able to help out :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1