I need to make table with number of rows equals to number of days in current month, can anyone help me with that? I need to create that dynamically using jquery or javascript.
You could do something like this:
function daysInMonth(month,year) {
return new Date(year, month, 0).getDate();
}
var $table = $('<table>');
var $tbody = $('<tbody>');
var days= daysInMonth(new Date().getMonth(), new Date().getYear());
$table.append($tbody);
for (i = 1; i<= days; i++){
var $tr = $('<tr>');
var $td = $('<td>');
$td.text(i);
$tr.append($td);
$tbody.append($tr);
};
$('body').append($table);
fiddle here: http://jsfiddle.net/nicolapeluchetti/WG88Y/
This creates a table with a row for each day of the month. i included a td with the current day.
This is the jQuery part of @dknaack 's answer:
var genTable = function () {
var $table = jQuery("<table />");
// days is equeal to @dhnaack 's answer
var days = DateTime.DaysInMonth(DateTime.Now.Date.Year, DateTime.Now.Date.Month);
jQuery(days).each(function(i,e) {
var $tr = jQuery("<tr />");
var $tr = jQuery("<td />")
$table.append($tr.append($td));
});
return $table;
}
Then you could use it like so:
jQuery("body").append(genTable());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With