viperfx07 is here to blog about hacking, cracking, website, application, android, and many more.

Tuesday, September 21, 2010

Week 8 Challenge: Javascript

5:42 PM Posted by viperfx07 No comments
This week's challenge is about JavaScript. Specifically, it's about a Date() function to how many days old you are and pop up this value in an alert box on an html page as it opens. Find out the codes below...


<!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" xml:lang="en" lang="en">
<head>
 <title>My Blog</title>
 <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
 <script type="text/javascript">
  //Reference: http://www.javascriptkit.com/javatutors/datedifference.shtml
  function calcDate()
  {
  var today = new Date();
  var mybirthday = new Date(2010, 9, 5); //month is 0-11 in Javascript

  //Set 1 day in milliseconds
  var one_day=1000*60*60*24;

  //Calculate difference btw the two dates, and convert to days 
  alert("You are "+Math.ceil((today.getTime()-mybirthday.getTime())/(one_day))+" days old!");
  }   
 </script>
</head>
<body onload="calcDate()">
</body>
</html>

0 comments:

Post a Comment