Saturday 28 May 2011

JavaScript Date Object




Date: Date is an object providing number of methods to get the current date and time information of client system.  To access the date object method, we need to create the instance.

Window property is by default in the ram execute. But for date object, first we need to load in ram and then we can access the functionality of date. This object is providing number of methods and every client side scripting language refers ‘0’ for January Sunday.




<script language="JavaScript" type=text/JavaScript>




var dayarray=new Array ("Sunday","Monday","Tuesday","Wednesday",
               "Thursday","Friday","Saturday")


var montharray=new Array("January","February","March","April","May","June",
               "July","August","September","October","November","December")


function getthedate(){
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var dn=""
if (hours>=12)
dn=""
if (hours>12){
hours=hours-12
}
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
//Hire change font size


var cdate="< small> < font color='ffffff' face='Arial'>"
  +dayarray[day]
  +", "+daym+" "+montharray[month]+", "+year
  +" "+hours+":"+minutes+":"+seconds+" "+dn
  +"  < /small>"


if (document.all)
document.all.clock.innerHTML=cdate
else if (document.getElementById)
document.getElementById("clock").innerHTML=cdate
else
document.write(cdate)
}
if (!document.all&&!document.getElementById)
getthedate()
function goforit(){
if (document.all||document.getElementById)
setInterval("getthedate()",1000)
}






</script>
<SPAN id=clock></SPAN>






To use this in your web pages, just include the above script somewhere, and call function goforit() in your <body> tag. For example:


 Collapse


<body leftMargin=0 topMargin=0 
  onload=goforit() marginwidth="0" marginheight="0">


It's very simple. Use it and enjoy.


Here are some samples


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).
setMinutes(minutes, [seconds], [millisec])
Sets the minutes of the Date object. (minutes: 0-59).
setSeconds(seconds, [millisec])
Sets the seconds of the Date object. (seconds: 0-59).
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();.





1 comment:

  1. That was too long. Is there a shorter way to organized the time/date system?

    ReplyDelete