JavaScript
does not provide a date data type like other programming languages,
however, you can create a date object, which contains date information:
variable = new Date("month, day, year, hours:minutes:seconds");
SomeDay = new Date("November, 3, 2003, 12:15:00");
or
variable = new Date(year, month, day, hours,
minutes, seconds);
SomeDay = new Date(2003, 10, 3, 12, 15, 0);
The first example is a string, while the second example uses numeric
values. Note that the month is 1 less than the current month because
JavaScript numbers the months 0 - 11. Hours are expressed as military
time. If you omit the date and time information, the script will return
the system's date and time.
Today = new Date();
The above, however, is not enough because JavaScript stores dates
and times as the number of milliseconds since 6 pm on 12/31/1969,
which isn't very useful. Fortunately, you can use the JavaScript date
methods to translate the numbers into useful dates. For each part
of the date you want displayed, you need a date method to retrieve
the value.
Retrieve date values
To retrieve the date value, you might use the ThisDay variable to
store the day of the month, the ThisMonth variable to store the month
and the ThisYear variable to store the year. To get that information,
you apply the applicable method to each of your variables:
var Today = new Date();
Day:
DayValue = DateObject.getDate(); as
ThisDay = Today.getDate();
Month:
MonthValue = DateObject.getMonth() + 1; as
ThisMonth = Today.getMonth() + 1;
Year:
YearValue = DateObject.getYear(); as
ThisYear = Today.getYear(); // For 2 digit year prior to 2000 and
4 digit year after or
ThisYear = Today.getFullYear(); //For 4 digit year - needed for correct
calculations!
Other methods:
getSeconds();
getMinutes();
getHours();
getDay();
Work
with Dates
Edit the npntxt.htm to add the following code:
<script language="JavaScript">
<!-- Hide from old browsers
var Today=new Date("November 3, 2003"); // Test date
var ThisDay=Today.getDate();
var ThisMonth=Today.getMonth()+1;
var ThisYear=Today.getFullYear();
var DaysLeft=52; // Test value, will have the script calculate later
document.write("Today is "+ThisMonth+"/"+ThisDay+"/"+ThisYear+"<br>");
document.write("Only "+DaysLeft+" days until Christmas");
// Stop hiding -->
</script>