Deborah's Date/Time ExamplesAdding the Current Date and TimeThis statement creates a variable called currentTime
var currentTime = new Date();This statement creates a variable called localTime and assigns the current local time. var localTime = now.toString();This statement creates a variable called GMTTime and assigns the Greenwich Mean Time (also referred to as UTC).
var GMTTime = now.toGMTString();These two statements output the results. The document.write statement is used to display text, formatting and any variables that are enclosed in the parenthesis. In the first statement the words "Local Time:" and the Bold tags are enclosed in quotes. The variable "localTime" is not enclosed in quotes but has plus signs before and after it to indicate that the contents of the variable should be added after the Local time label and that the HTML Break tag should be included after it.
document.write("<B>Local time:</B> " + localTime + "<BR>");This is the result.
Now add a clock display. Here is the code: var currentTime = new Date();The variables are hours, mins and secs. The built-in functions are getHours, getMinutes and getSeconds.
Let's display the date in the format MM/D/YYYY. Here is the code for this format. var currentTime = new Date();In this example, it is important to note that you have to add 1 to the getMonth() function. Also, since the year is calculated from January 1, 1900, you have to add 1900 to the getYear() function.
|