Instead of using a STATIC DATE for your website or application, it is better to use a DYNAMIC DATE with a little bit of JavaScript. For example, you could include a dynamic year in your copyright notice in your footer so that when another year rolls around, it gets updated automatically (e.g., © 2012-20XX where 20XX gets updated the first of ever year without you having to worry about it or forgetting to update it).
NOTE: This tutorial uses the same JavaScript that will be taught. So if you come back at another time or date, the content will be updated.
DynamicDateStamp_Beginner (FINAL)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dynamic Date Stamp - Beginner </title>
</head>
<body>
</body>
</html>
<body>
<div id="dateStamp">
</div>
</body>
<div id="dateStamp">NOTE: The today variable has been assigned to the new instance of the Date class using the new constructor keyword. The document.write() method is used to "write" whatever is in the parenthesis (in this case, the date) to the page at the very spot the script is written.
<script>
var today = new Date();
document.write(today);
</script>
</div>
<div id="dateStamp">NOTE: The toDateString() method returns the FIRST HALF of the Date Object (today). It is one of many methods available to the Date Object as will be seen in the Intermediate Version.
<script>
var today = new Date();
document.write(today.toDateString());
</script>
</div>
If you change .toDateString() to .toTimeString(), you will get the SECOND HALF of the today object:
<style>
#dateStamp{
border: 5px solid red;
padding: 20px;
display: inline;
font-family: "Arial";
font-weight: bold;
font-size: 36px;
color: chartreuse;
background-color: black;
border-radius: 20px;
text-align: center;
margin: 100px;
}
</style>
</head>
In version one of the dynamic Date Stamp you are LIMITED to what format the object will give you. Version two is similar to the beginner version except the days of the week and months of the year are formatted better.
NOTE: This tutorial uses the same JavaScript that will be taught. So if you come back at another time or date, the content will be updated.
DynamicDateStamp_Intermediate (FINAL)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dynamic Date Stamp - Intermediate</title>
</head>
<body>
</body>
</html>
<body>
<div id="dateStamp">
</div>
</body>
<div id="dateStamp">
<script>
</script>
</div>
<script>
var today = new Date();
document.write(today);
</script>
var today = new Date();NOTE: The .toDateString(); method will return the date portion of the Date Object.
document.write(today.toDateString());
document.write(today.getMonth());
document.write(today.getFullYear());
var today = new Date();
var dayName = today.getDay(); // DAY of the week - Monday, Tuesday, etc.
var monthName = today.getMonth(); // MONTH of the year - January, February, etc.
var dayOfWeek = ["Sunday", "Monday","Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
var month = ["January", "February", "March", "April", "May","June",
"July", "August", "September", "October", "November", "December"];
var today = new Date();
var dayName = today.getDay(); // DAY of the week - Monday, Tuesday, etc.
var monthName = today.getMonth(); // MONTH of the year - January, February, etc.
document.write(dayOfWeek[dayName]);
document.write(month[monthName]);
document.write(dayOfWeek[dayName]+ ", " +
month[monthName] + " " + today.getDate() + ", " + today.getFullYear());
</script>
OPTIONAL: Alternatively, you can replace the document.write(...) statement with the following code:
var FormattedDate = dayOfWeek[dayName]NOTE: Some developers prefer to write code in this manner. Notice the code was assigned to a variable (FormattedDate) and then except for the FIRST LINE, the variable is the same but has "+=" instead of "=" and ends with a semi-colon (;).
FormattedDate += ", ";
FormattedDate += month[monthName];
FormattedDate += " ";
FormattedDate += today.getDate();
FormattedDate += ", ";
FormattedDate += today.getFullYear();
document.write(FormattedDate);
</script>
<style>
font-size: 36px;
font-family: "Arial";
color: #0F0;
background-color: black;
border: 14px groove gray;
padding: 10px;
text-align:center;
margin: 15px;
width: 450px;
</style>
</head>
var day = today.getDay();
var suffix = "th";
if (day==1 || day==21 || day==31) {suffix = "st"};
if (day==2 || day==22) {suffix = "nd"};
if (day==3 || day==23) {suffix = "rd"};
document.write(dayOfWeek[dayName]+ ", " +
month[monthName] + " " + today.getDate() + "<sup>" + suffix + "</sup>" + ", "
+ today.getFullYear());
NOTE: A new variable (day) is create to ascertain the number for the day of the week. Three "if" statements are used to determine what suffix to add to the end of the day and the <sup> element is used to create a superscript.
The third version is similar to the Intermediate version. However, the CSS and JavaScript code will be externalize and the script element that was embedded in the body of the page will be move to the head element.
DynamicDateStamp_Advanced (FINAL)
<div id="dateStamp">
<script src="DynamicDateStamp_Advanced.js"></script>
</div>
// document.write();
dayOfWeek[dayName] + ", " +
month[monthName] + " " + today.getDate() + "<sup>" + suffix +
"</sup>" + ", " + today.getFullYear();
var formattedDateStamp = dayOfWeek[dayName]+ ", " +
month[monthName] + " " + today.getDate() + "<sup>" + suffix +
"</sup>" + ", " + today.getFullYear();
var formattedDateStamp = dayOfWeek[dayName]+ ", " +
month[monthName] + " " + today.getDate() + "<sup>" + suffix +
"</sup>" + ", " + today.getFullYear();
document.getElementById("dateStamp").innerHTML = formattedDateStamp;
NOTE: This will get a reference to the div element with the id of dateStamp and write the result of the formattedDateStamp variable to it.
<script src="DynamicDateStamp_Advanced.js"></script>
</head>
window.onload = function() {NOTE: This is a common "wrapper' that is used so that the HTML PAGE GETS LOADED FIRST SO THAT IT CAN GET REFERENCES TO ANY HTML ELEMENTS THAT IS NEEDS BEFORE ANY JAVASCRIPT CODE IS EXECUTED.
};
window.onload = function() {
[MOVE ALL OF THE CODE HERE....]
};
The Date Object is choke full of time-related methods to create timers, clocks, etc. In this tutorial, we will create a Digital Clock using the Date Object along with a timer method called setInterval().
NOTE: This tutorial uses the same JavaScript that will be taught. So if you come back at another time, the content will be updated.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Digital Clock - Expert</title>
</head>
<body>
</body>
</html>
<body>
<div id="clock">12:00:00 PM</div>
</body>
@font-face
{
font-family:digital;
src:url("digital-7 (mono).ttf");
}
#clock {
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
color: #17D4FE;
font-size: 150px;
font-family:digital;
background-color: black;
letter-spacing: 7px;
width:900px;
text-align: center;
border: 5px solid #17D4FE;
border-radius: 30px;
-webkit-box-shadow: 6px 10px 15px #070000;
box-shadow: 6px 10px 15px #070000;
}
<title>Digital Click - Expert</title>
<link rel="stylesheet" href="clock_styles.css">
<script src="DigitalClock_Expert.js"></script>
</body>
function updateClock() {
// Create a new instance (object) of the Date Class and store a reference in the currentTime variable
var currentTime = new Date();
// Get current Hours, Minutes, and Seconds ----------------------------------------------
var currentHours = currentTime.getHours(); // 0 - 23
var currentMinutes = currentTime.getMinutes(); // 0 - 59
var currentSeconds = currentTime.getSeconds(); // 0 - 59
// Concatenate strings to format time -------------------------------------------------------
var currentTimeFormat = currentHours + ":" + currentMinutes + ":" + currentSeconds;
// Update the clock -------------------------------------------------------------
document.getElementById("clock").innerHTML = currentTimeFormat;
}
updateClock();
NOTE: The updateClock(); statement is used to INVOKE the updateClock() function above.it.
// Update the clock -----------------------------------------------------------NOTE: The setTimeout() method is a JavaScript built-in method that requires two arguments--a function and a interval time. It will execute any function a infinite number of times based on the second argument. In this case, 1000ms or 1 second. Any built-in function is global and can be used anywhere in your code and does not have to be assigned to a variable. (e.g., var myVar = setTimeout(function, time); ). Also, notice the updateClock function in the setTimeout method DOES NOT have parenthesis.
document.getElementById("clock").innerHTML = currentTimeFormat;
setTimeout(updateClock, 1000);
}
updateClock();
// Get current Hours, Minutes, and Seconds ----------------------------------------------
var currentHours = currentTime.getHours(); // 0 - 23
var currentMinutes = currentTime.getMinutes(); // 0 - 59
var currentSeconds = currentTime.getSeconds(); // 0 - 59
// Convert to 12-hour format by subtracting 12 ----------------------------------
if (currentHours > 12)
{
currentHours = currentHours - 12; // (e.g., 20 = 20-12 = 8)
// You could write the above line like this: currenntHours -= 12
}
// Alternatively, you can write the code like this:
// currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;
NOTE: This "if" statement checks to see if the hour is greater than 12 and if so SUBSTRACT 12 from the hour.
// Convert to 12-hour format by subtracting 12 ----------------------------------NOTE: This 'if" statement checks to see if the mid-hour is zero. If so CHANGE it to 12.
if (currentHours > 12) {
currentHours = currentHours - 12; // (e.g., 20 = 20-12 = 8)
// You could write the above line like this: currenntHours -= 12
}
// Alternatively, you can write the code like this:
// currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;
// Convert mid-hours from "0" to "12" ---------------------------------------------------------------------------------
if (currentHours == 0) {
currentHours = 12;
}
// Alternatively, you can write the code like this:
// currentHours = (currentHours == 0) ? 12 : currentHours;
// Convert mid-hours from "0" to "12" ----------------------------------------------
if (currentHours == 0) {
currentHours = 12;
}
// Alternatively, you can write the code like this:
// currentHours = (currentHours == 0) ? 12 : currentHours;
// Add leading zero to hours, minutes, and seconds when they are less than ten -----
currentHours = (currentHours < 10 ? "0" : "") + currentHours;
currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;
NOTE: This "if" statement checks to see if a number is single digit and if so ADD a leading zero to it. You can comment out the currentHours line if you prefer.
ADD CLOCK HERE...
// Get current Hours, Minutes, and Seconds ----------------------------------------------NOTE: Because the 24 hours format is the default, any thing over 12 (13-24) will be convert to the PM format.
var currentHours = currentTime.getHours(); // 0 - 23
var currentMinutes = currentTime.getMinutes(); // 0 - 59
var currentSeconds = currentTime.getSeconds(); // 0 - 59
var AMorPM = " AM";
// Convert to 12-hour format by subtracting 12 -----------------------------------------
if (currentHours > 12) {
currentHours = currentHours - 12; // (e.g., 20 = 20-12 = 8)
AMorPM = " PM";
// You could write the above line like this: currenntHours -= 12
}
// Concatenate strings to format time ------------------------------------------
var currentTimeFormat = currentHours + ":" + currentMinutes + ":" + currentSeconds + AMorPM;
}
sup{color:white;
font-size:50px;
}
NOTE: sup is short of superscript.
// Concatenate strings to format time ------------------------------------------
var currentTimeFormat = currentHours + ":" + currentMinutes + ":" + currentSeconds + "<sup>" + AMorPM + "</sup>";