• Welcome to Valhalla Legends Archive.
 

Javascript Date Time

Started by Imperceptus, May 05, 2008, 11:41 AM

Previous topic - Next topic

Imperceptus

Trying to Get JavaScript Date Time.  Perhaps im going about this wrong. but heres what im doing(well trying to make work).

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>testtitle>
  <script type="text/javascript" src="inc/yumjava.js"></script>
  <script language="JavaScript" type="text/javascript">
    alert(CurrentDateTime);
  </script>
</head>
<body>
</body>
</html>


Contents of yumjava.js
function CurrentDate(){
  var currentTime = new Date();
  var month = currentTime.getMonth() + 1;
  var day = currentTime.getDate();
  var year = currentTime.getFullYear();
  var current = month + "/" + day + "/" + year;
  return current;
}

function CurrentTime(){
  var currentTime = new Date();
  var hours = currentTime.getHours();
  var minutes = currentTime.getMinutes();

  if (minutes < 10){
    minutes = "0" + minutes;}
  var current = hours + ":" + minutes + " ";
  if(hours > 11){
    current = current + "PM";
  } else {
    current = current + "AM";
  return current;
  }
}

function CurrentDateTime(){
  var current = CurrentDate;
  current = current + CurrentTime;
  return current;
}


Why wont the alert work?

Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

Barabajagal

Well, for starters, you're only returning the time if it's AM (and not removing 12 hours for PM anyway O.o), your <title> tags are broken, and when you call functions, you need to put () after the function name. If I were writing it, it'd look more like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>test</title>
  <script type="text/javascript" src="inc/yumjava.js"></script>
  <script type="text/javascript">
   alert (CurrentDateTime());
  </script>
</head>
<body>
</body>
</html>

function CurrentDate()
{
var currentTime = new Date();
var month       = currentTime.getMonth() + 1;
var day         = currentTime.getDate();
var year        = currentTime.getFullYear();
return (month + "/" + day + "/" + year);
}
function CurrentTime()
{
var currentTime = new Date();
var hours       = currentTime.getHours();
var minutes     = currentTime.getMinutes();
if (minutes < 10)
{
   minutes = "0" + minutes;
}
if(hours > 11)
{
  if(hours == 12)
  {
   return ("12:" + minutes + " PM");
  }
  else
  {
   return ((hours - 12) + ":" + minutes + " PM");
  }
}
else
{
  if(hours == 0)
  {
   return ("12:" + minutes + " AM");
  }
  else
  {
   return ((hours) + ":" + minutes + " AM");
  }
}
}
function CurrentDateTime()
{
return (CurrentDate() + " " + CurrentTime());
}

Imperceptus

#2
Whoops on the title. I copied and pasted the head of the page. didn't see reason to post the entire thing.  but thats good to know about the java things will check em out. thanks for help.  I personally dont really use java much. mainly asp/.net. how unfortunate that it wasn't an option this time. Yeah I see your point on the not removing hours, lol. Im posting in 24 hour time but still denoting am or pm roflmao. dee dee dee.
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.