Thursday, March 29, 2012

get month and year in jquery

Earlier i explained how to Jquery Get next and previous month. Here i will show how to get the current month/Date name from month index using java script. For this i have taken one array variable with month names .Then the result month index will be passed to month names array.Here the resulted month name is assigned to Div using present() function.If we want to get the current year there is an option to get the year i:e getFullYear();
<html>
<head>
<script>
var cdt = new Date();
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var current_month = cdt.getMonth();
var month_name = monthNames[current_month];
var current_year =cdt.getFullYear();
     
function present() {
var cdt = new Date();
var month = monthNames[current_month];
var DivObj = document.getElementById('testmonth');
DivObj.innerHTML = month+','+" " + current_year;
}
<script></head>
<body><input type="submit" value="GetMonth" onclick="present()"/>
</body></html>
by using the above function we can get the current date by using Date functionality in jquery
var currentdate = cdt.getDate();

No comments:

Bel