DevelopPHP
home gear home donate
Search Results For
in:

Libraries book

HTML Library CSS Library JavaScript Library PHP Library

Videosbook

PHP and MySQL JavaScript HTML and CSS Vector and 3D Flash Actionscript Android Dev Miscellaneous

Resourcesbook

Website Hosting Developer Q+A
Sprite Programming Mastery
Date Object Javascript

The Javascript Date object sports a large set of built-in methods for writing code that can get, set, calculate and render dates and times. Date objects should be created using the new operator in order to return actual date objects instead of strings.

Formats for establishing dates and times

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

Javascript CODE EXAMPLE
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.

Javascript CODE EXAMPLE
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 of the Date Object

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)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getDate();

 

getDay - returns local day of the week as 0 - 6 (0 = sunday, 1 = monday, etc...)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getDay();

 

getFullYear - returns local full year for a date object

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getFullYear();

 

getHours - returns the local hours for a date object (0 - 23)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getHours();

 

getMilliseconds - returns local milliseconds for a date object (0 - 999)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getMilliseconds();

 

getMinutes - returns local minutes for a date object (0 - 59)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getMinutes();

 

getMonth - returns local month for a date object (0 - 11)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getMonth();

 

getSeconds - returns local seconds for a date object (0 - 59)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getSeconds();

 

getTime - returns local total milliseconds for the date object referenced from Jan 1, 1970

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getTime();

 

getTimezoneOffset - returns local timezone offset from universal time, in minutes

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getTimezoneOffset();

 

getUTCDate - returns date of the month according to UTC (1 - 31)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getUTCDate();

 

getUTCDay - returns day of the week according to UTC (0 - 6)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getUTCDay();

 

getUTCFullYear - returns full year for a date object according to UTC

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getUTCFullYear();

 

getUTCHours - returns hours for a date object according to UTC (0 - 23)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getUTCHours();

 

getUTCMilliseconds - returns milliseconds for a date object according to UTC (0 - 999)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getUTCMilliseconds();

 

getUTCMinutes - returns minutes for a date object according to UTC (0 - 59)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getUTCMinutes();

 

getUTCMonth - returns month for a date object according to UTC (0 - 11)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getUTCMonth();

 

getUTCSeconds - returns seconds for a date object according to UTC (0 - 59)

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.getUTCSeconds();

 

setDate - sets local date of the month for a date object (1 - 31)

Javascript CODE EXAMPLE
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

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setFullYear(1986);

 

setHours - sets local hours for a date object (0 - 23)

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setHours(17);

 

setMilliseconds - sets local milliseconds of a date object (0 - 999)

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setMilliseconds(500);

 

setMinutes - sets local minutes for a date object (0 - 59)

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setMinutes(40);

 

setMonth - sets local month for a date object (0 - 11)

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setMonth(0); // January

 

setSeconds - sets local seconds for a date object (0 - 59)

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setSeconds(30);

 

setTime - sets total milliseconds for the date object referenced from Jan 1, 1970

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setTime(1356411600000); // xmas 2012

 

setUTCDate - sets month for a date object according to UTC (1 - 31)

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setUTCDate(10);

 

setUTCFullYear - sets full year for a date object according to UTC

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setUTCFullYear(2000);

 

setUTCHours - sets hours for a date object according to UTC (0 - 23)

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setUTCHours(18);

 

setUTCMilliseconds - sets milliseconds for a date object according to UTC (0 - 999)

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setUTCMilliseconds(250);

 

setUTCMinutes - sets minutes for a date object according to UTC (0 - 59)

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setUTCMinutes(45);

 

setUTCMonth - sets month for a date object according to UTC (0 - 11)

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setUTCMonth(11);

 

setUTCSeconds - sets seconds for a date object according to UTC (0 - 59)

Javascript CODE EXAMPLE
var myDate = new Date();
myDate.setUTCSeconds(30);

 

toDateString - returns a human readable date portion of a date object, as a string

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.toDateString();

 

toISOString - returns a date string in ISO format

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.toISOString();

 

toLocaleDateString - returns a human readable local date only, as a string

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.toLocaleDateString();

 

toLocaleString - returns a human readable local full date and time, as a string

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.toLocaleString();

 

toLocaleTimeString - returns local time only, as a string

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.toLocaleTimeString();

 

toString - converts a date object to a string object

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.toString();

 

toTimeString - returns local time and timezone without the date portion, as a string

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.toTimeString();

 

toUTCString - returns universal date and time, as a string

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.toUTCString();

 

valueOf - returns the primitive millisecond value of a date object

Javascript CODE EXAMPLE
var myDate = new Date();
var result = myDate.valueOf();

 

 

Properties of the Date Object

constructor - returns the constructor function used for the object

Javascript CODE EXAMPLE
var myDate = new Date();
document.write(myDate.constructor);

 

Tutorials On File Covering Date Object Usage

Javascript Digital Clock Tutorial CSS Custom Animated Clock on Your Website
Javascript Date Time Countdown Tutorial Christmas 2012 Doomsday

 

Home   •   Terms of Use   •   T-Shirts   •   RSS   •   Testimonials   •   Subscribe   •   Classroom Chalkboard   •   Developer Q+A   •   Donate   •   Top ↑
Popular In PHP / MySQL E-Commerce Store Production
Social Network Website Building
Image Upload / Photo Processing
CMS Software Programming
Mass Email Systems
Magic XML Data
Search Programming
Popular In JavaScript JSON Programming
Ajax Programming
Animating Elements
WYSIWYG Programming
Date/Time Programming
DOM Scripting
Object Reference
Popular In HTML Canvas Element
New Form Elements
Audio Element
Video Element
Drag and Drop
Event Handling
Element Grouping
Popular In CSS Custom Font Embedding
Dynamic Fit Backgrounds
Theatre Mode
Box Overlays
CSS Level 1 Properties
CSS Level 2 Properties
CSS Level 3 Properties
DevelopPHP.com is a growing educational system packed with video and written material that you can access 24/7 100% free. The focus here in 2014 is spread between five technologies: HTML, CSS, JavaScript, PHP and MySQL. Learn programming theory, database interaction, web design, animation, graphics editing, vector art, 3D modeling, and much more. The educational experience here is a hybrid one offering both high quality video tutorials and code libraries that grow more robust every day. The code libraries are being assembled so that developers can discover all of the building blocks of their favorite languages. The libraries coupled with high quality video tutorials will take you deep down the rabbit hole very quickly. You can also ask questions and get free advice at WebIntersect.com.
HTML5