0

How can I display all dates in a month @ Below format

All days in current month: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... 31

HTML

<div id="allDates"></div>

jQuery

$(document).ready(function(){
    var d = new Date();
    var allDates = d.getDate();
    $('#allDates').html('All days in current month:' + '<br>' + allDates);
});
Reddy
  • 1,477
  • 29
  • 79

4 Answers4

3

Using the date constructor generate the first date of a month and last date of month and then push all dates in an array and display it.

$(document).ready(function(){
    var today = new Date(),
    firstDay = new Date(today.getFullYear(), today.getMonth(), 1),
    lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0),
    result = [];

    while(firstDay <= lastDay){
      result.push(firstDay.getDate());
      firstDay.setDate(firstDay.getDate() + 1);
    }

    $('#allDates').html('All days in current month:' + '<br>' + result.join(','));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="allDates"></div>

Using this stackoverflow solution

function daysInMonth(month,year) {
    return new Date(year, month, 0).getDate();
}

$(document).ready(function(){
    var today = new Date(),
    days = daysInMonth(today.getMonth() + 1, today.getFullYear()),
    result = Array.from({length:days}, (_,i) => i+1).join(',');
    $('#allDates').html('All days in current month:' + '<br>' + result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="allDates"></div>
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
2

var d = new Date();
var LastDay = 32 - new Date(d.getFullYear(), d.getMonth(), 32).getDate();
var Output = "";
for (var i = 0; i < LastDay - 1; i++) {
    Output += (i + 1) + ", ";
}
document.write(Output);
pitaridis
  • 2,801
  • 3
  • 22
  • 41
0

Use this

$(document).ready(function(){
  var d = new Date();
    var numberOfDays = getDaysInMonth(d.getMonth() + 1, d.getFullYear())
    var allDates = ''
  for (var i = 1; i <= numberOfDays; i++) {
    allDates += i + ' '
  }

  $('#allDates').html('All days in current month:' + '<br>' + allDates);
});
function getDaysInMonth(month,year) {
  return new Date(year, month, 0).getDate();
}
Dinesh Patil
  • 1,042
  • 10
  • 13
0

Here is another twist, get the last day of the month and then loop to get the output as

var getDaysInMonth = function(month,year) {
  // Here January is 1 based
  //Day 0 is the last day in the previous month
 return new Date(year, month, 0).getDate();
// Here January is 0 based
// return new Date(year, month+1, 0).getDate();
};

$(document).ready(function(){
  var d = new Date();
    var lastdayOfmonth = getDaysInMonth(d.getMonth() , d.getFullYear())
    var allDates = ''
  for (var i = 1; i <= lastdayOfmonth; i++) {
    allDates += i + ' '
  }

  $('#allDates').html('Current month dates :' + '<br>' + allDates);
});

Please refer this link :- https://www.w3resource.com/javascript-exercises/javascript-date-exercise-3.php

Ajay2707
  • 5,690
  • 6
  • 40
  • 58