|
CodeAve.com - JavaScript - Document Info | |||
|
|
|||
| Date/Time Formats | ||||
|
|
||||
<html> <title>CodeAve.com(JavaScript: Date and Time Formats)</title> <body bgcolor="#FFFFFF"> <script langauge="JavaScript"> <!-- // Demonstrates the raw info derived form date() // Create a varible with the current data info var right_now=new Date(); document.write("<b>Date() Raw Values</b><br>"); document.write("var right_now=new Date();<br>") document.write("Date() = "); document.write(Date()); document.write("<br>"); document.write("right_now.getYear() = "); document.write(right_now.getYear()); document.write("<br>"); document.write("right_now.getMonth()+1 = "); document.write(right_now.getMonth()+1); document.write("<br>"); document.write("right_now.getDate() = "); document.write(right_now.getDate()); document.write("<br>"); document.write("right_now.getHours() = "); document.write(right_now.getHours()); document.write("<br>"); document.write("right_now.getMinutes() = "); document.write(right_now.getMinutes()); document.write("<br>"); document.write("right_now.getSeconds() = "); document.write(right_now.getSeconds()); document.write("<br>"); // --> </script> <p> <script langauge="JavaScript"> <!-- // Demonstrates the typical format for date and time info derived form date() document.write("<b>Date() Typical Format</b><br>"); // Create a varible with the current data info var right_now=new Date(); // Month always comes through as one numeric // Less than the current month. Jan=0 Feb=1 etc. document.write(right_now.getMonth()+1); document.write("/"); document.write(right_now.getDate()); document.write("/"); // Year can come as the current year // or the number of years since 1900 // To account for this we check the value var right_year=right_now.getYear(); if (right_year < 2000) right_year = right_year + 1900; document.write( right_year ); document.write(" "); // Begin Ouptut of Time // Hours come in military time // To put in civilian time you must check the hours // To see if they're greater than 12 var right_hours=right_now.getHours() if (right_hours > 12) right_hours = right_hours - 12; document.write(right_hours); document.write(":"); // To display leading zeros before the minutes // Check for minutes less then 10 var right_min=right_now.getMinutes(); if (right_min < 10) document.write("0"); document.write(right_min); document.write(":"); // To display leading zeros before the seconds // Check for minutes less then 10 var right_sec=right_now.getSeconds(); if (right_sec < 10) document.write("0"); document.write(right_sec); // To display AM or PM assign a varible to A.M. Value // If the the hours are greater than 12 then switch // the value to P.M. var ampm=" A.M."; if (right_now.getHours() > 12) ampm=" P.M."; document.write(ampm); // --> </script> </body> </html>
|
||||
|
|
||||