Display Date Time on Web Page

 

Display Date Time on Web Page

Use JavaScript to display date time on web page. Displaying the date and time within your web page is a good way to spice up your website and give it a more professional appearance.

var tmonth=new Array(“January”,”February”,”March”,”April”,”May”,”June”,”July”,”August”,”September”,”October”,”November”,”December”);

function GetClock(){
var d=new Date();
var nmonth=d.getMonth(),ndate=d.getDate(),nyear=d.getYear();
if(nyear<1000) nyear+=1900;

var nhour=d.getHours(),nmin=d.getMinutes(),ap;
if(nhour==0){ap=" AM";nhour=12;}
else if(nhour12){ap=” PM”;nhour-=12;}

if(nmin<=9) nmin="0"+nmin;

document.getElementById('webpageclock').innerHTML=""+tmonth[nmonth]+" "+ndate+", "+nyear+" "+nhour+":"+nmin+ap+"";
}

window.onload=function(){
GetClock();
setInterval(GetClock,1000);
}

 

Date and Time

To display the date and time within your web page, place the following code within your HTML where you would like the date and time to appear.

<script type="text/javascript">
var tmonth=new Array("January"
,"February","March","April","May","June","July","August","September","October","November","December");

function GetClock(){
var d=new Date();
var nmonth=d.getMonth(),ndate=d.getDate(),nyear=d.getYear();
if(nyear<1000) nyear+=1900;

var nhour=d.getHours(),nmin=d.getMinutes(),ap;
if(nhour==0){ap=" AM";nhour=12;}
else if(nhour12){ap=" PM";nhour-=12;}

if(nmin<=9) nmin="0"+nmin;

document.getElementById('webpageclock').innerHTML=""+tmonth[nmonth]+" "+ndate+", "+nyear+" "+nhour+":"+nmin+ap+"";
}

window.onload=function(){
GetClock();
setInterval(GetClock,1000);
}
</script>
<div id="webpageclock" style="font:14pt Arial; color:#FF0000;"></div>

Edit the text in bold to suit your needs.

 
 
The following JavaScript code will display the date on your web page in the following format:


 
Place this code where you would like the date to appear:

<script language="JavaScript">
<!– Script courtesy of http://www.web-source.net – Your Guide to Professional Website Design and Development
var today_date= new Date()
var month=today_date.getMonth()+1
var today=today_date.getDate()
var year=today_date.getFullYear()
//document.write("Today’s date is: ")
document.write(month+"/"+today+"/"+year)
//–>
</script>

 
To change the font style and color, enclose the above JavaScript code with your font tags. Or, better yet, use CSS.

You can change the date format from this:

 
To this:


 
Or this:


 

Locate the following line in the code:

document.write(month+”/”+today+”/”+year)

Change it to one of the following codes.

If you’d like your date to be separated by a dash, use this code:

document.write(month+”-“+today+”-“+year)

If you’d like your date to be separated by a period, use this code:

document.write(month+”.”+today+”.”+year)

This concludes the Display Date on Web Page code.

 
 

 
 
Visit the following pages for a wealth of copy and paste codes and tips:

HTML Tips
Copy and paste special effect HTML codes for your web page.

HTML Codes
An HTML codes chart you can use to copy and paste codes into your web page.

HTML Color Codes
An HTML color codes chart you can use to copy and paste color codes into your web page.

JavaScript Codes
Copy and paste special effect JavaScript codes for your web page.

Web Design Tips
Tips, tricks, and special effect codes for your web page.

HTML Tags
A list of HTML tags.

HTML 5 Tags
A list of the latest HTML tags to be developed.

ASCII Codes
Special text characters and code for your web page.