Forum : To display date and Time in javascript
Brief description  about Online courses   join in Online courses
View rashmi r patil 's Profile

To display date and Time in javascript

Hello sir, I am doing the first assignment of javascript,I am not getting how to display today's date and time. Plz guide me
Asked by rashmi r patil | Nov 5, 2010 |  Reply now
Replies (2)
View santosh hegde 's Profile
Hi,
you can try this to display today`s date&time included month and year.

in the Head section please add below code:
<!-- To Display month date and time -->
<script type="text/javascript">
tday =new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
tmonth=new Array("January","February","March","April","May","June","July","August","September","October","November","December");

function GetClock(){
d = new Date();
nday = d.getDay();
nmonth = d.getMonth();
ndate = d.getDate();
nyear = d.getYear();
nhour = d.getHours();
nmin = d.getMinutes();
nsec = d.getSeconds();

if(nyear<1000) nyear=nyear+1900;

if(nhour == 0) {ap = " AM";nhour = 12;}
else if(nhour <= 11) {ap = " AM";}
else if(nhour == 12) {ap = " PM";}
else if(nhour >= 13) {ap = " PM";nhour -= 12;}

if(nmin <= 9) {nmin = "0" +nmin;}
if(nsec <= 9) {nsec = "0" +nsec;}


document.getElementById('clockbox').innerHTML=""+tday[nday]+", "+tmonth[nmonth]+" "+ndate+", "+nyear+" "+nhour+":"+nmin+":"+nsec+ap+"";
setTimeout("GetClock()", 1000);
}
window.onload=GetClock;
</script>

Just add below code in "Body" section:
<div id="clockbox"></div>


In the abow code you will get output fo eg: Saturday,November 13 2010 8:25:04 PM.

and you can give style to display like this:
<div id="clockbox" class="content"></div>
for abow code should declare style in CSS as .content or Name it as you want..
just try it out..

Nov 13, 2010
View teacher siliconindia 's Profile
Hi Rashmi,

Create date object, and display. Eg.

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

u can format ur output or u can display selected values date object using date methods,

Method Description

getDate() Returns the day of the month (from 1-31)
getDay() Returns the day of the week (from 0-6)
getFullYear() Returns the year (four digits)
getHours() Returns the hour (from 0-23)
getMilliseconds() Returns the milliseconds (from 0-999)
getMinutes() Returns the minutes (from 0-59)
getMonth() Returns the month (from 0-11)
getSeconds() Returns the seconds (from 0-59)
getTime() Returns the number of milliseconds since midnight Jan 1, 1970
getTimezoneOffset() Returns the time difference between GMT and local time, in minutes
getUTCDate() Returns the day of the month, according to universal time (from 1-31)
getUTCDay() Returns the day of the week, according to universal time (from 0-6)
getUTCFullYear() Returns the year, according to universal time (four digits)
getUTCHours() Returns the hour, according to universal time (from 0-23)
getUTCMilliseconds() Returns the milliseconds, according to universal time (from 0-999)
getUTCMinutes() Returns the minutes, according to universal time (from 0-59)
getUTCMonth() Returns the month, according to universal time (from 0-11)
getUTCSeconds() Returns the seconds, according to universal time (from 0-59)
getYear() Deprecated. Use the getFullYear() method instead and many more
Nov 10, 2010