Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Tuesday, February 14, 2012

Printing the Today's Date using JavaScript

<html>
<head>
<title>
</title>
<script type="text/javascript">

function getTodaysDate()
    {
         var today=new Date(); // Initialize Date in raw form
var date=today.getDate(); // Get the numerical date
var year=today.getYear(); // Get the year
var day = today.getDay(); // Get the day in number form (0,1,2,3,etc.)
var month=today.getMonth()+1; // Get the month
var suffix="";
// Make day number value correspond to actual day name
var dayName=new Array(7)
dayName[0]="Sunday";
dayName[1]="Monday";
dayName[2]="Tuesday";
dayName[3]="Wednesday";
dayName[4]="Thursday";
dayName[5]="Friday";
dayName[6]="Saturday";

// Add suffix to date (1st, 2nd, 4th, etc.)
if (date==1) suffix=("st");
else if (date==2) suffix=("nd");
else if (date==3) suffix=("rd");
else if (date==21) suffix=("st");
else if (date==22) suffix=("nd");
else if (date==23) suffix=("rd");
else if (date==31) suffix=("st");
else suffix=("th");
var monthName="";
// Make month number correspond to month name
if (month==1) monthName=("January");
else if (month==2) monthName=("February");
else if (month==3) monthName=("March");
else if (month==4) monthName=("April");
else if (month==5) monthName=("May");
else if (month==6) monthName=("June");
else if (month==7) monthName=("July");
else if (month==8) monthName=("August");
else if (month==9) monthName=("September");
else if (month==10) monthName=("October");
else if (month==11) monthName=("November");
else monthName=("December");

//document.write(dayName[day] + ", " + monthName + " " + date + suffix + ", " + year);
document.getElementById("divDate").innerHTML=date + suffix+" "+ monthName+" " + year;

    }


</script>


</head>
<body onload="getTodaysDate();">
<div id="divDate"></div>
</body>
</html>



No comments:

Post a Comment