How To Add Date To Web Page Or Blog

Posted by Suraj Kayastha in Articles, Internet

In this tutorial I am going to help you to add dynamically current date in your blog/webpage. This will help you make your livelier.

We’ll be using Javascript to initiate the current date and time.

.getDay(), .getMonth(), .getDate(), .getFullYear() functions will be calling the current day, date, month and year.

We’ll be using the help of variable d for new date. Variable weekday will have maximum Array, 7 which is the number of days in a week and variable month with maximum array 12.

Then we are defining each days in weekday[X]=Name of Day.

Similary for month mnth[X]=Name of month serially

After the variables have been addressed we’ll be calling javascript to get current date using document.write() function.

For example, (“” + weekday[d.getDay()]); will call current Day in numerical format starting from 0 as Sunday and 6 as Saturday.

Same goes for the month as well.

When the codes are properly arranged you’ll get the javascript code enclosed in <script type=’text/javascript’> tag ready like this:

<script type=’text/javascript’>

var d=new Date();

var weekday=new Array(7);

weekday[0]=“Sunday”;

weekday[1]=“Monday”;

weekday[2]=“Tuesday”;

weekday[3]=“Wednesday”;

weekday[4]=“Thursday”;

weekday[5]=“Friday”;

weekday[6]=“Saturday”;

var m=new Date();

var mnth=new Array(12);

mnth[0]=“January”;

mnth[1]=“February”;

mnth[2]=“March”;

mnth[3]=“April”;

mnth[4]=“May”;

mnth[5]=“June”;

mnth[6]=“July”;

mnth[7]=“August”;

mnth[8]=“September”;

mnth[9]=“October”;

mnth[10]=“November”;

mnth[11]=“December”;

document.write(““ + weekday[d.getDay()]);

document.write(“, “ + mnth[d.getMonth()]);

document.write(“ “ +[d.getDate()]);

document.write(“ “ +[d.getFullYear()]);

</script>

Tada… this code works individually in any browser.

If you don’t have time, just copy the above code and paste it where you want.

View Demo Here

Hope you liked it. Keep visiting Hackspc for more interesting articles like this.