Friday, 27 August 2010

JavaScript DateTime

Included in JavaScript is a Date Object. This object is a part of the overall specification of JavaScript and is available to any page that can include JavaScript (these days, that pretty much means any page.) To use it, you create a new date object instance by setting a variable as follows:

ourDate = new Date( )

"ourDate" can be any variable name you wish. This statement (known as a Date Constructor) creates an instance of the Date Object, named ourDate. In its raw form like this, it contains the exact time (as accurately as the local computer knows it) represented in milliseconds past 00:00 on January first, 1970. It's quite a big number! By handling dates and times in this manner, the arithmetic involved in date calculations is greatly simplified. All you need is an easy way to convert back and forth -- and the Date( ) object includes methods for just that purpose. Through these methods, the Date( ) object understands the following date and time fields:

year, month, day, hours, minutes, seconds, milliseconds

The Date( ) object doesn't have properties that can be directly read and written. Instead it has methods that are used to work with the data value in the object. This is a technical difference that stems from the way the date and time is stored. It's not possible to read the month directly, for example, it must be derived from the data by calculation. Calculation requires a method. The Date( ) object includes quite a lot of methods; here's an alphabetically ordered list:
getDate( ) return the day of the month
getDay( ) return the day of the week
getFullYear( ) return the four digit year
getHours( ) return the hours
getMilliseconds( ) return the milliseconds
getMinutes( ) return the minutes
getMonth( ) return the month
getSeconds( ) return the seconds
getTime( ) return the internal representation of the time
getTimezoneOffset( ) return the offset from GMT
getUTCDate( ) return the Universal Time day of the month
getUTCDay( ) return the Universal Time day of the week
getUTCFullYear( ) return the Universal Time four digit year
getUTCHours( ) return the Universal Time hours
getUTCMilliseconds( ) return the Universal Time milliseconds
getUTCMinutes( ) return the Universal Time minutes
getUTCMonth( ) return the Universal Time month
getUTCSeconds( ) return the Universal Time seconds


setDate( ) set the day of the month
setDay( ) set the day of the week
setFullYear( ) set the four digit year
setHours( ) set the hours
setMilliseconds( ) set the milliseconds
setMinutes( ) set the minutes
setMonth( ) set the month
setSeconds( ) set the seconds
setTime( ) set the date and time using the millisecond format
setUTCDate( ) set the Universal Time day of the month
setUTCDay( ) set the Universal Time day of the week
setUTCFullYear( ) set the Universal Time four digit year
setUTCHours( ) set the Universal Time hours
setUTCMilliseconds( ) set the Universal Time milliseconds
setUTCMinutes( ) set the Universal Time minutes
setUTCMonth( ) set the Universal Time month
setUTCSeconds( ) set the Universal Time seconds


toGMTstring( ) return the date and time as a string, using the GMT time zone
toLocaleString( ) return the date and time as a string, using the local time zone
toString( ) return the date and time as a string
toUTCString( return the date and time as a string, using Universal Time


valueOf( ) convert a date to the internal millisecond format

Two other methods, getYear( ) and setYear( ), also exist but are deprecated in favor of the "Full" versions listed above.

The constructor also has two methods that you can invoke. These are Date.parse( ) and Date.UTC( )



small Example as follows

function ak() {
ourDate = new Date();
alert("ak");
document.write(ourDate.getDate());


var myDate = new Date();
var today = new Date();

myDate.setFullYear(2010, 7, 27);

today.setMilliseconds(001);
myDate.setMilliseconds(001);

document.write("My Date" + myDate.getMilliseconds());
document.write("Today" + "
" + today.getMilliseconds() + "
");

document.write("My Date" + myDate + "
");
document.write("Today" + today + "
");

var tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);
document.write("tomorrow" + tomorrow + "
");
tomorrow.setHours(0);
tomorrow.setMinutes(0, 0, 0);


document.write("tomorrow" + tomorrow + "
");


if (myDate == tomorrow) {
alert("equal");
}

if (myDate >= tomorrow) {
alert("my date is greate than or equel today");
}
else {
alert("my date is less than today");
}


}






Date Object ourDate =
new Date(); - Fri Aug 27 2010 10:28:40 GMT+0100 (GMT Daylight Time)


ourDate.toLocaleString() - 27 August 2010 11:34:36

ourDate.toString() - Fri Aug 27 2010 11:36:13 GMT+0100 (GMT Daylight Time)

ourDate.toGMTstring() -

No comments:

Post a Comment