Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

Date

The JavaScript Date object sports a set of built-in methods that can get, set, calculate and render dates and times.

Formats for establishing dates and times

Establish a date object representing the always current local date and time:

var today = new Date(); document.write(today);

Acceptable formats for establishing date objects that represent a specific date and time in the past or future. The examples below all target Christmas 2012, with the time of day being set to 6:30 AM where specified.

var xmas = new Date("Dec 25, 2012"); var xmas = new Date("December 25, 2012"); var xmas = new Date("12/25/2012"); var xmas = new Date("2012,12,25"); var xmas = new Date(2012,11,25); var xmas = new Date("December 25, 2012 06:30:00"); var xmas = new Date("12/25/2012 06:30:00"); var xmas = new Date("2012,12,25,06:30:00"); var xmas = new Date(2012,11,25,6,30,0); var xmas = new Date(1356411600000);

Methods

Local - local time zone in relation to where the user is located in the world. UTC(coordinated universal time) - the world's time standard for regulating clocks and time.

getDate - returns local date of the month for a date object (1 - 31). var myDate = new Date(); var result = myDate.getDate(); getDay - returns local day of the week as 0 - 6 (0 = sunday, 1 = monday, etc...). var myDate = new Date(); var result = myDate.getDay(); getFullYear - returns local full year for a date object. var myDate = new Date(); var result = myDate.getFullYear(); getHours - returns the local hours for a date object (0 - 23). var myDate = new Date(); var result = myDate.getHours(); getMilliseconds - returns local milliseconds for a date object (0 - 999). var myDate = new Date(); var result = myDate.getMilliseconds(); getMinutes - returns local minutes for a date object (0 - 59). var myDate = new Date(); var result = myDate.getMinutes(); getMonth - returns local month for a date object (0 - 11). var myDate = new Date(); var result = myDate.getMonth(); getSeconds - returns local seconds for a date object (0 - 59). var myDate = new Date(); var result = myDate.getSeconds(); getTime - returns local total milliseconds for the date object referenced from Jan 1, 1970. var myDate = new Date(); var result = myDate.getTime(); getTimezoneOffset - returns local timezone offset from universal time, in minutes. var myDate = new Date(); var result = myDate.getTimezoneOffset(); getUTCDate - returns date of the month according to UTC (1 - 31). var myDate = new Date(); var result = myDate.getUTCDate(); getUTCDay - returns day of the week according to UTC (0 - 6). var myDate = new Date(); var result = myDate.getUTCDay(); getUTCFullYear - returns full year for a date object according to UTC. var myDate = new Date(); var result = myDate.getUTCFullYear(); getUTCHours - returns hours for a date object according to UTC (0 - 23). var myDate = new Date(); var result = myDate.getUTCHours(); getUTCMilliseconds - returns milliseconds for a date object according to UTC (0 - 999). var myDate = new Date(); var result = myDate.getUTCMilliseconds(); getUTCMinutes - returns minutes for a date object according to UTC (0 - 59). var myDate = new Date(); var result = myDate.getUTCMinutes(); getUTCMonth - returns month for a date object according to UTC (0 - 11). var myDate = new Date(); var result = myDate.getUTCMonth(); getUTCSeconds - returns seconds for a date object according to UTC (0 - 59). var myDate = new Date(); var result = myDate.getUTCSeconds(); setDate - sets local date of the month for a date object (1 - 31). var myDate = new Date("April 23, 2012"); myDate.setDate(29); // changes from April 23 to April 29 setFullYear - sets local full year for a date object. var myDate = new Date(); myDate.setFullYear(1986); setHours - sets local hours for a date object (0 - 23). var myDate = new Date(); myDate.setHours(17); setMilliseconds - sets local milliseconds of a date object (0 - 999). var myDate = new Date(); myDate.setMilliseconds(500); setMinutes - sets local minutes for a date object (0 - 59). var myDate = new Date(); myDate.setMinutes(40); setMonth - sets local month for a date object (0 - 11). var myDate = new Date(); myDate.setMonth(0); // January setSeconds - sets local seconds for a date object (0 - 59). var myDate = new Date(); myDate.setSeconds(30); setTime - sets total milliseconds for the date object referenced from Jan 1, 1970. var myDate = new Date(); myDate.setTime(1356411600000); // xmas 2012 setUTCDate - sets month for a date object according to UTC (1 - 31). var myDate = new Date(); myDate.setUTCDate(10); setUTCFullYear - sets full year for a date object according to UTC. var myDate = new Date(); myDate.setUTCFullYear(2000); setUTCHours - sets hours for a date object according to UTC (0 - 23). var myDate = new Date(); myDate.setUTCHours(18); setUTCMilliseconds - sets milliseconds for a date object according to UTC (0 - 999). var myDate = new Date(); myDate.setUTCMilliseconds(250); setUTCMinutes - sets minutes for a date object according to UTC (0 - 59). var myDate = new Date(); myDate.setUTCMinutes(45); setUTCMonth - sets month for a date object according to UTC (0 - 11). var myDate = new Date(); myDate.setUTCMonth(11); setUTCSeconds - sets seconds for a date object according to UTC (0 - 59). var myDate = new Date(); myDate.setUTCSeconds(30); toDateString - returns a human readable date portion of a date object, as a string. var myDate = new Date(); var result = myDate.toDateString(); toISOString - returns a date string in ISO format. var myDate = new Date(); var result = myDate.toISOString(); toLocaleDateString - returns a human readable local date only, as a string. var myDate = new Date(); var result = myDate.toLocaleDateString(); toLocaleString - returns a human readable local full date and time, as a string. var myDate = new Date(); var result = myDate.toLocaleString(); toLocaleTimeString - returns local time only, as a string. var myDate = new Date(); var result = myDate.toLocaleTimeString(); toString - converts a date object to a string object. var myDate = new Date(); var result = myDate.toString(); toTimeString - returns local time and timezone without the date portion, as a string. var myDate = new Date(); var result = myDate.toTimeString(); toUTCString - returns universal date and time, as a string. var myDate = new Date(); var result = myDate.toUTCString(); valueOf - returns the primitive millisecond value of a date object. var myDate = new Date(); var result = myDate.valueOf();

JavaScript

Objects

ArgumentsArrayAudioBooleanDataTransferDateDocumentElementFunctionGlobalHistoryHTMLElementMathNavigatorNumberObjectRegExpScreenStorageStringWindowXMLHttpRequest