Categories:

JavaScript Kit > JavaScript Reference > Here

Date Object

Last updated: April 16th, 2014

There are fours ways of instantiating a date object:

new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds) //most parameters here are optional. Not specifying causes 0 to be passed in.

Here are a few examples of instantiating a date:

today = new Date()
birthday = new Date("March 11, 1985 09:25:00")
birthday = new Date(85,2,11)
birthday = new Date(85,2,11,9,25,0)

Note: Be sure to check out our Date examples.

Related Tutorials

Properties

Properties Description
prototype Use this property to attach additional properties and/or methods that get reflected in all instances of the Date object.

Methods

Note: "[]" surrounding a parameter below means the parameter is optional.

Methods Description
getFullYear() Returns year in full 4 digit format (ie: 2004).
getYear() Returns the year, in 4 digit format or otherwise depending on browser. Deprecated in favor of getFullYear().
getMonth() Returns the month. (Range is 0-11)!
getDate() Returns the day of the month (Range is 1-31)
getDay() Returns the day of the week (Range is 0-6). 0=Sunday, 1=Monday, etc.
getHours() Returns the hour (Range is 0-23).
getMinutes() Returns the minutes. (Range is 0-59).
getSeconds() Returns the seconds. (Range is 0-59).
getMilliseconds() Returns the milliseconds. (Range is 0-999).
getTime() Returns the millisecond representation of the current Date object. In the words, the number of milliseconds between 1/1/1970 (GMT) and the current Date object.
getTimezoneOffset() Returns the offset (time difference) between Greenwich Mean Time (GMT) and local time of Date object, in minutes.
getUTCFullYear() Returns the full 4 digit year in Universal time.
getUTCMonth() Returns the month in Universal time.
getUTCDate() Returns the day of the month in Universal time.
getUTCDay() Returns the day of the week in Universal time.
getUTCHours() Returns the hour in Universal time.
getUTCMinutes() Returns the minutes in Universal time.
getUTCSeconds() Returns the seconds in Universal time.
getUTCMilliseconds() Returns the milliseconds in Universal time.
   
setFullYear(year, [month], [day]) Sets the year of the Date object. (year: 4 digit year).
setYear(year) Sets the year of the Date object. "year" can be two digits (1900 is automatically added to it), or 4 digits. Deprecated over setFullYear() above.
setMonth(month, [day]) Sets the month of the Date object. (month: 0-11)
setDate(day_of_month) Sets the day of the month of the Date object. (day_of_month: 1-31).
setHours(hours, [minutes], [seconds], [millisec]) Sets the hour of the Date object. (hours: 0-23), and optionally the lesser time fields at the same time. The following sets a date object instance to the very beginning of today, at midnight:

var midnight = new Date()
midnight.setHours(0,0,0,0) // set date instance to midnight today

Contrast that with the following, which sets the date to the start of the next upcoming day instead (midnight):

var midnight = new Date()
midnight.setHours(24,0,0,0) // set date instance to next upcoming midnight

 

setMinutes(minutes, [seconds], [millisec]) Sets the minutes of the Date object. (minutes: 0-59), and optionally the lesser time fields at the same time.
setSeconds(seconds, [millisec]) Sets the seconds of the Date object. (seconds: 0-59), and optionally the lesser time fields at the same time.
setMilliseconds(milli) Sets the milliseconds field of the Date object. (milli: 0-999)
setTime(milli) Sets the value of the Date object in terms of milliseconds elapsed since 1/1/1970 GMT.
setUTCFullYear(year, [month], [day]) Sets the year of the Date object in Universal time.
setUTCMonth(month, [day]) Sets the month in Universal time.
setUTCDate(day_of_month) Sets the day of the month in Universal time.
setUTCHours(hours, [minutes], [seconds], [millisec]) Sets the hours in Universal time.
setUTCMinutes(minutes, [seconds], [millisec]) Sets the minutes in Universal time.
setUTCSeconds(seconds, [millisec]) Sets the seconds in Universal time.
setUTCMilliseconds(milli) Sets the milliseconds in Universal time.
   
toGMTString() Converts a date to a string, using the GMT conventions. Deprecated in favor of toUTCString().
toLocaleString() Converts a date to a string, using current locale conventions.
toLocaleDateString() Returns the date portion of the Date as a string, using current locale conventions.
toLocaleTimeString() Returns the time portion of the Date as a string, using current locale conventions.
toString() Converts a Date to human-readable string.
toUTCString() Converts a Date to human-readable string, in Universal time.
parse(datestring) Returns the number of milliseconds in a date string since 1/1/1970. (datestring: a string containing the date/time to be parsed).
UTC(year, month, [day], [hours], [minutes], [seconds], [milli]) Returns the number of milliseconds in a date string since 1/1/1970, Universal time.
valueOf() Converts a Date to milliseconds. Same as getTime();.

Examples

Example 1- write out current date

To write out today's date for example, you would do:

<script type="text/javascript">

var mydate= new Date()
var theyear=mydate.getFullYear()
var themonth=mydate.getMonth()+1
var thetoday=mydate.getDate()

document.write("Today's date is: ")
document.write(theyear+"/"+themonth+"/"+thetoday)

</script>

Output:

Example 2- calculate day of week of a date

Below example returns the day of the week a specific past date falls on:

birthday = new Date(1978,2,11)
weekday = birthday.getDay()
alert(weekday) //alerts 6, or Saturday

Example 3- calculate number of days expired

This example calculates the number of days passed since the Year 2000:

var today=new Date()
var year2000=new Date(2000,0,1)
var diff=today-year2000 //unit is milliseconds
diff=Math.round(diff/1000/60/60/24) //contains days passed since Year 2000

Example 4- set date to be a future date

This example sets the Date object to be 3 days into the future:

var today=new Date()
today.setDate(today.getDate()+3) //today now is set to be 3 days into the future


Reference List

Right column

CopyRight (c) 2018 JavaScript Kit. NO PART may be reproduced without author's permission. Contact Info