JS Date Object


Date object initialization:

var d = new Date(); Current date and time
var d = new Date(milliseconds); //from 1/1/1970
var d = new Date(datestring); //"m,d,y","1,2,2000"
var d = new Date(year, month, day, hours,
minutes, seconds, milliseconds);
//month [0-11]


getDate(), setDate() method: get or set the day of the month (1 - 31)

var d = new Date();
alert(d.toDateString()); //current date
alert(d.getDate()); //day of the month
d.setDate(24);
alert(d.toDateString());



getDay() method: get the day of the week (0 - 6)

var d = new Date();
alert(d.getDay()); //week day of today



getFullYear(), setFullYear() method: get or set the year (4 digits)

var d = new Date();
alert(d.getFullYear()); //this year
d.setFullYear(2010);
alert(d.toDateString()); //this day in 2010



getHours(), setHours() method: get or set the hours (0-23)

var d = new Date();
alert(d.getHours()); //this hour
d.setHours(1);
alert(d.getHours()); //1:00 am today



getMinutes(), setMinutes(): get or set the minutes (0-59)

getMilliseconds(), setMinutes(): get or set the milliseconds (0-999)

getSeconds(), setMinutes(): get or set the seconds (0-59)

getMonth(), setMonth(): get or set the month (0-11)

var d = new Date();
alert(d.getMonth()); //this month
d.setMonth(0);
alert(d.toDateString()); //January this day



getTime(),setTime() method: get or set the milliseconds since the midnight 1/1/1970

var d = new Date();
alert(d.getTime());
d.setTime(d.getTime() + 1000 * 3600 * 24 * 365);
//Almost one year later
alert(d.toDateString());



toDateString() method: convert the date into a string

var d = new Date();
alert(d.toDateString());



toISOString() method: convert into ISO standard string

var d = new Date();
alert(d.toISOString());



toJSON() method: convert into JSON format

var d = new Date();
alert(d.toJSON());



toString() method: convert into string

var d = new Date();
alert(d.toString());



toLocaleString() method: convert into string using locale conventions

var d = new Date();
alert(d.toLocaleString());



toLocaleDateString() method: convert the date portion into string in locale conventions

var d = new Date();
alert(d.toLocaleDateString());


Note: ToLocaleDateString() may not work properly in Chrome browser.

toUTCString() method: convert into string according to UTC time

var d = new Date();
alert(d.toUTCString());



toLocaleTimeString() method: convert the time portion into string in locale conventions

var d = new Date();
alert(d.toLocaleTimeString());


Note: ToLocaleTimeString() may not accurate in Chrome browser.

toTimeString() method: convert the time portion into string

var d = new Date();
alert(d.toTimeString());



valueOf() method: returns the primitive date:

var d = new Date();
alert(d.valueOf());



getTimezoneOffset() method: gets the timezone difference between UTC and local Time, in minutes. The offset is positive if the local timezone is behind UTC and negative if it is ahead. Daylight savings are being counted. For example, the EDT time zone is UTC-4, 240 will be returned. :

var d = new Date();
alert(d.getTimezoneOffset());



parse() static method: get millisecond since midnight 1/1/1970 from a date string

var ms = Date.parse("April,7,2000");//955080000000


setUTCDate(): set the day of month according to UTC time
setUTCFullYear(): set the year according to UTC time
setUTCHours(): set the Hours according to UTC time
setUTCMilliseconds(): set milliseconds according to UTC time
setUTCMonth(): set month according to UTC time
setUTCSeconds(): set seconds according to UTC time


constructor property: returns the date constructing prototype:

var d = new Date();
var x = d.constructor;
//x = "function Date() {[native code]}"


prototype property: add property or method to Date object. Following example add one year to the Date object.

Date.prototype.addoneyear = function()
{
var yr = this.getFullYear() + 1;
this.setFullYear(yr);
}
var d = new Date("10,8,2000");
alert(d.toDateString()); //Sun Oct 08 2000
d.addoneyear();
alert(d.toDateString()); //Mon Oct 08 2001


convert.idontcarewhatyouthink.net © 2021  Terms of Use | Home