Difference between revisions of "JavaScript Date Object"

From Techotopia
Jump to: navigation, search
(Creating a JavaScript Date Object)
Line 2: Line 2:
  
 
The Date object enables the JavaScript develper to create a Date object initialized to a specific time and date, or to create a date object that reflects the current ''system time'' on the computer on which the browser is running. There are two important things to note when working with the JavaScript Date object. Firstly,  when reading the date and time of the user's computer you are completely at the mercy of the user's ability to set and maintain the correct day and time on their computer. Secondly, whilst you can read the system date and time set on the user's computer, and change those settings within your Date object instance, you ''cannot'' change the computer's system date and time. No matter what methods you call or properties you change on your Date object, the user's system date and time remain unaltered.
 
The Date object enables the JavaScript develper to create a Date object initialized to a specific time and date, or to create a date object that reflects the current ''system time'' on the computer on which the browser is running. There are two important things to note when working with the JavaScript Date object. Firstly,  when reading the date and time of the user's computer you are completely at the mercy of the user's ability to set and maintain the correct day and time on their computer. Secondly, whilst you can read the system date and time set on the user's computer, and change those settings within your Date object instance, you ''cannot'' change the computer's system date and time. No matter what methods you call or properties you change on your Date object, the user's system date and time remain unaltered.
 +
 +
== Understanding System Time ==
 +
 +
Rather than understanding the concepts of dates and times, computers essentially record the passage of time since a particular baseline date and time (often referred to as the ''epoch''). The epoch date, and unit of measurement for elapsed time depends on the system being used. A Windows system, for example, counts the number of 100-nanoseconds since January 1, 1601 00:00:00. A UNIX or LINUX based system, on  the other hand, counts the number of seconds that have elapsed since January 1, 1970 00:00:00.
 +
 +
When working with JavaScript it is useful to know that when the current date is requested it is returned as the number of milliseconds that have elapsed since January 1, 1970, 00:00:00. As you will see later in this chapter, the JavaScript Date object contains plenty of methods that make it easy for developers to set and read the time using human readable date formats.
  
 
== Creating a JavaScript Date Object ==
 
== Creating a JavaScript Date Object ==

Revision as of 13:52, 8 May 2007

The JavaScript Date object enables you to work with, and manipluate time (and no, it doesn't enable you to travel back through time to buy a lottery ticket with this week's winning numbers - if it did that I would have already done it, and would be sitting on beach somewhere instead of writing this book).

The Date object enables the JavaScript develper to create a Date object initialized to a specific time and date, or to create a date object that reflects the current system time on the computer on which the browser is running. There are two important things to note when working with the JavaScript Date object. Firstly, when reading the date and time of the user's computer you are completely at the mercy of the user's ability to set and maintain the correct day and time on their computer. Secondly, whilst you can read the system date and time set on the user's computer, and change those settings within your Date object instance, you cannot change the computer's system date and time. No matter what methods you call or properties you change on your Date object, the user's system date and time remain unaltered.

Understanding System Time

Rather than understanding the concepts of dates and times, computers essentially record the passage of time since a particular baseline date and time (often referred to as the epoch). The epoch date, and unit of measurement for elapsed time depends on the system being used. A Windows system, for example, counts the number of 100-nanoseconds since January 1, 1601 00:00:00. A UNIX or LINUX based system, on the other hand, counts the number of seconds that have elapsed since January 1, 1970 00:00:00.

When working with JavaScript it is useful to know that when the current date is requested it is returned as the number of milliseconds that have elapsed since January 1, 1970, 00:00:00. As you will see later in this chapter, the JavaScript Date object contains plenty of methods that make it easy for developers to set and read the time using human readable date formats.

Creating a JavaScript Date Object