I have the following code (shortened) which creates the dates of a month while looping through an array.
This works fine so far but I am struggling to stop it if it reaches the last day of a month.
I am using this to dynamically add table rows to an HTML table and as the array is set to end at 31 it always starts with the next month when the set month has less than 31 days. The cells in my table then show e.g. ...28, 29, 1, 2
for the February column.
Can someone tell me how I can check whether day1
, day2
or day3
are the last day of the month and if set them to '' ?
My thought was that the following will give me the last day of a month but I couldn't find a way to work this in here and to use it properly for the parisons:
var d = new Date(y, m+1, 0);
My JS (shortened):
$('#btnStart').on('click', function(){
var date1, day1;
var date2, day2;
var d = 0;
var m = 0; // user input
var y = 2015; // user input
for (d = 0; d <= 30; d++) {
date1 = new Date(y, m, d+1);
day1 = date1.getDate();
date2 = new Date(y, m+1, d+1);
day2 = date2.getDate();
}
});
Update: This is only needed within a given year so it will never go from December to January.
Many thanks for any help with this, Mike
I have the following code (shortened) which creates the dates of a month while looping through an array.
This works fine so far but I am struggling to stop it if it reaches the last day of a month.
I am using this to dynamically add table rows to an HTML table and as the array is set to end at 31 it always starts with the next month when the set month has less than 31 days. The cells in my table then show e.g. ...28, 29, 1, 2
for the February column.
Can someone tell me how I can check whether day1
, day2
or day3
are the last day of the month and if set them to '' ?
My thought was that the following will give me the last day of a month but I couldn't find a way to work this in here and to use it properly for the parisons:
var d = new Date(y, m+1, 0);
My JS (shortened):
$('#btnStart').on('click', function(){
var date1, day1;
var date2, day2;
var d = 0;
var m = 0; // user input
var y = 2015; // user input
for (d = 0; d <= 30; d++) {
date1 = new Date(y, m, d+1);
day1 = date1.getDate();
date2 = new Date(y, m+1, d+1);
day2 = date2.getDate();
}
});
Update: This is only needed within a given year so it will never go from December to January.
Many thanks for any help with this, Mike
Share Improve this question edited Jun 2, 2015 at 10:22 keewee279 asked Jun 2, 2015 at 9:55 keewee279keewee279 1,6546 gold badges34 silver badges63 bronze badges4 Answers
Reset to default 3If you add one day to the date, and the resulting date is the first of next month, then the examined date was the last day of its month.
something like
isLast = (new Date(examinedDate.getTime() + 24*60*60*1000).getDate()) === 1
You should use date.getTime() to pare dates.
Working jsbin with your example code:
http://jsbin./puhuvirize/2/edit?js,output
This will give you the last day of month 'm':
new Date(y, m + 1, 0)
Add this in your For loop
if(day2 < day1)
break;
with a simple function perhaps:
function isLastDay(y, m, d)
{
var d1 = (m < 11) ? new Date(y, m + 1, 0) : new Date(y, 0, 0);
var d2 = new Date(y, m, d);
return (d1.getTime() === d2.getTime());
}