CodeAve.com CodeAve.com - JavaScript - Document Info
ASP
JavaScript
·Document Info
·Forms
·Images
·Miscellaneous
·Navigation
·Script Writers
·Contents
·What's New?
CSS
HTML
Maps
Links
Mail List
Search
Sitemap


 


Date/Time Formats
Displaying the current date and time is a popular use for JavaScript, however getting them to display the way you would like it to can often times be confusing. The value for year can easily vary from browser to browser, either displaying the current year or displaying the number of years since 1900. To account for this a simple tautology is placed in the code to see if the value is greater than 2000 and to make adjustments. Also, the first month of the year starts with a zero. To get the current month to display you will need to add a one to the value that is contained in the month field.
View the Output
Text View
Print View
Mail this Link
Download the Code

<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("&nbsp;");

// 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>

 

 

 



ASP: What's New? | Articles | Script Writers | Database Display | Read/Write
Server Variables | Response Objects | Random Events | Miscellaneous
HTML: Forms | Hyperlinks | Headers | Tables | Hyperlinks | Headers | Text Display
JavaScript: Document Info | Forms | Images | Navigation | Script Writers
CSS: Basics | Page Display | Text Display | Script Writers | Miscellaneous
Maps: Map Script Writers | Bing Maps | Google Maps
Privacy Statement

CodeAve.com is hosted by MyHosting.com
Donate Food Online with a Mouse Click at TheHungerSite.com
Donate Land Online with a Mouse Click at TheRainForestSite.com
© 1999 - 2010 CodeAve.com
All Rights Reserved