Javascript Date Object Constructor with Examples

Create Date Object Best Example in Javascript

Date Object manipulate dates and times. To create a Date object, use one of the following five syntax:
new Date(); //Object Constructor
new Date(milliseconds)
new Date(datestring);
new Date(year, month, day);
new Date(year, month, day, hours, minutes, seconds)


With no arguments, the Date() constructor creates a Date object set to the current date and time
Otherwise, the arguments to Date() specify the date, and, optionally, the time for the new object. Note that in the third through fifth syntax above, the specified times are interpreted as local times, not
Greenwich Mean Time (GMT) times.

Date() can also be used as a function instead of a constructor. When invoked without the new operator, and without any arguments, Date() does not create a Date object but instead returns a string that represents the current date and time for the local time zone. Calling Date() without the new operator is equivalent to using it with the new operator to create a Date object and then using the toString()
method of that object to convert the date to a string

When used as a constructor, the arguments to Date(), are the following:

Arguments

milliseconds
The number of milliseconds between the desired date and midnight GMT on January 1st, 1970. For example passing the argument 5000 would create a date that represents five seconds past midnight on 1/1/70.

datestring
A single argument that specifies the date and, optionally, the time as a String. The string should have the format month day, year hours:minutes:seconds where month is the English name of the month, and hours are specified in 24-hour format. For example: new Date("December 31, 1999 23:59:59");
The seconds field or the entire time specification may be omitted from this format.

year
The year, in four-digit format. For example, specify 2001 for the year 2001. For years within the 20th century, you can also subtract 1900 and specify the date in two-digit format. For example, you could specify 97 for the year 1997.

month
The month, specified as an integer from 0 (January) to 11 (December)
day The day of the month, specified as an integer from 1 to 31. Note that this argument uses 1 as its lowest value, while other arguments use 0 as their lowest value.

hours
The hour, specified as an integer from 0 (midnight) to 23 (11 p.m.).
minutes The minutes in the hour, specified as an integer from 0 to 59.
seconds The seconds in the minute, specified as an integer from 0 to 59.

Once a Date object is created by using Date() as a constructor in any of the forms shown above, any of the following methods can be used to operate on the Date object. Note that unlike most JavaScript objects, the Date object has no properties that can be read and written directly; instead, all access to date and time fields is done through these methods.

getDate()
Return the day of the month of a Date object.
getDay()
Return the day of the week of a Date object.
getHours()
Return the hours field of a Date object.
getMinutes()
Return the minutes field of a Date object.
getMonth()
Return the month field of a Date object.
getSeconds()
Return the seconds field of a Date object.
getTime()
Return the internal, millisecond representation of a Date object.
getTimezoneOffset()
Return the time zone difference, in minutes, between this date and GMT.
getYear()
Return the year field of a Date object.
parse()
Parse a string representation of a date, and return it in millisecond format.
setDate()
Set the day of the month field of a Date object.
setHours()
Set the hour field of a Date object.
setMinutes()
Set the minutes field of a Date object.
setMonth()
Set the month field of a Date object.
setSeconds()
Set the seconds field of a Date object.
setTime()
Set the fields of a Date object using the millisecond format.
setYear()
Set the year field of a Date object.
toGMTString()
Convert a Date to a string, using the GMT time zone.
toLocaleString()
Convert a Date to a string, using the local time zone.
UTC()
Convert a numeric date and time specification to millisecond format.


Workout Yourself

<SCRIPT language="JavaScript">
today = new Date(); make a note of today's date
christmas = new Date(); get a date with the current year
christmas.setMonth(11); set the month to December...
christmas.setDate(25); ...and the day to the 25th
// If Christmas hasn't already passed, compute the number of
// milliseconds between now and Christmas, then convert this
// to a number of days and print a message.
if (today.getTime() < christmas.getTime()) {
difference = christmas.getTime() - today.getTime();
difference = Math.floor(difference / (1000 * 60 * 60 * 24));
document.write('Only ' + difference + ' days until Christmas!<P>');
}
</SCRIPT>
... rest of HTML document here ..
<SCRIPT language="JavaScript">
// Here we use Date objects for timing. We divide by 1000
// to convert milliseconds to seconds. We could divide
// further to convert to minutes, hours or days.
now = new Date();
document.write('<P>It took ' +
(now.getTime()-today.getTime())/1000 +
'seconds to load this page.');
</SCRIPT>


Related Topic How to Create the Dynamic Folder Tree Links Navigation
Javascript Spell Checker Embedded Code, Text Area Object
Developer Code for JS Timer
How to Add Text File or Code in a JSP File at Translation Time
JavaScript Date Formatting to String mm dd yyyy hh mm ss zz pp

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback