Valhalla Legends Forums Archive | Web Development | Javascript Date Time

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

[code]<!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>[/code]

Contents of yumjava.js
[code]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;
}[/code]

Why wont the alert work?

May 5, 2008, 4:41 PM
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:
[code]<!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>[/code]
[code]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());
}[/code]
May 5, 2008, 10:28 PM
Imperceptus
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.
May 6, 2008, 11:39 AM

Search