Java Reference
In-Depth Information
It's unlikely that you'll be using this way of defining a Date object very often, but this is how
JavaScript actually stores the dates. The other formats for giving a date are simply for convenience.
The third way for you to declare a Date object is to pass a string representing a date, or a date and
time. In the following example, you have "31 January 2014" :
var theDate3 = new Date("31 January 2014");
However, you could have written 31 Jan 2014 , Jan 31 2014 , or any of a number of valid variations
you'd commonly expect when writing down a date normally—if in doubt, try it out.
If you are writing your web pages for an international audience, you need to be aware of the different
ways of specifying dates. In the United Kingdom and many other places, the standard is day, month,
year, whereas in the United States the standard is month, day, year. This can cause problems if you
specify only numbers—JavaScript may think you're referring to a day when you mean a month.
In the fourth and final way of defining a Date object, you initialize it by passing the following parameters
separated by commas: year, month, day, hours, minutes, seconds, and milliseconds. For example:
var theDate4 = new Date(2014,0,31,15,35,20,20);
tip It's very easy to make a mistake when specifying a month using either the
third or fourth method of declaring a Date object. The easiest way to avoid
such headaches is to always use the name of the month where possible. That
way there can be no confusion.
This date is actually 31 January 2014 at 15:35:20 and 20 milliseconds. You can specify just the date
part if you want and ignore the time. Something to be aware of is that in this instance January is
month 0 , not month 1 , as you'd expect, and December is month 11 .
Getting Date Values
It's all very nice having stored a date, but how do you get the information out again? Well, you just
use the get methods. These are summarized in the following table:
method
returns
getDate()
The day of the month
getDay()
The day of the week as an integer, with Sunday as 0, Monday
as 1, and so on
getMonth()
The month as an integer, with January as 0, February as 1,
and so on
getFullYear()
The year as a four‐digit number
toDateString()
Returns the full date based on the current time zone as a
human‐readable string, for example, “Wed 31 Dec 2003”
 
Search WWH ::




Custom Search