Get current month and year in jQuery

getFullYear() method return current year and (getMonth() + 1) method return the current month whereas getDate() method return current day in jQuery.

Current year in jQuery

getFullYear() return the current year.

Current month in jQuery

getMonth() + 1 return the current month. Be careful getMonth() return index of current month, So always use getMonth() + 1 instead of getMonth()

Current day in jQuery

getDate() return the current day.

var currentYear = (new Date).getFullYear();
var currentMonth = (new Date).getMonth() + 1;
var currentDay = (new Date).getDate();

Note: you can also get month name instead of number. Please Click Here.

CODE
<!DOCTYPE html>
<html>
<head>
  <title>jQuery With Example</title>
  <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      $('.btnGetCurrentYear').click(function (event) {
        var currentYear = (new Date).getFullYear();
        alert(currentYear);
      });

      $('.btnGetCurrentMonth').click(function (event) {
        var currentMonth = (new Date).getMonth() + 1;
        alert(currentMonth);
      });

      $('.btnGeCurrentDay').click(function (event) {
        var currentDay = (new Date).getDate();
        alert(currentDay);
      });
    });
  </script>
</head>
<body>
  <div>
    <button class="btnGetCurrentYear">Get Current Year</button><br />
    <button class="btnGetCurrentMonth">Get Month Year</button><br />
    <button class="btnGeCurrentDay">Get Current Day</button>
  </div>
</body>
</html>
DISPLAY


3 comments :

  1. it showing client date not server date

    ReplyDelete
    Replies
    1. JavaScript run on client side that why it return the client system date.

      If you want server date then you post an ajax request and return the server date as result.

      Delete