How can I obtain the last day of the month with the timestamp being 11:59:59 PM?
function LastDayOfMonth(Year, Month) {
return new Date((new Date(Year, Month, 1)) - 1);
}
console.log(LastDayOfMonth(2009, 11))
Example:
> LastDayOfMonth(2009, 11)
Mon Nov 30 2009 23:59:59 GMT+0100 (CET)
This will give you last day of current month.
var t= new Date();
alert(new Date(t.getFullYear(), t.getMonth() + 1, 0, 23, 59, 59));
var d = new Date();
console.log(d);
d.setMonth(d.getMonth() + 1); // set month as next month
console.log(d);
d.setDate(0); // get the last day of previous month
console.log(d);
Here is output from the code above:
Thu Oct 03 2013 11:34:59 GMT+0100 (GMT Daylight Time)
Sun Nov 03 2013 11:34:59 GMT+0000 (GMT Standard Time)
Thu Oct 31 2013 11:34:59 GMT+0000 (GMT Standard Time)
var d = new Date();
m = d.getMonth(); //current month
y = d.getFullYear(); //current year
alert(new Date(y,m,1)); //this is first day of current month
alert(new Date(y,m+1,0)); //this is last day of current month
var month = 1; // 1 for January
var d = new Date(2015, month, 0);
console.log(d); // last day in January
Sometimes all you have is a text version of the current month, ie: April 2017.
//first and last of the current month
var current_month = "April 2017";
var arrMonth = current_month.split(" ");
var first_day = new Date(arrMonth[0] + " 1 " + arrMonth[1]);
//even though I already have the values, I'm using date functions to get year and month
//because month is zero-based
var last_day = new Date(first_day.getFullYear(), first_day.getMonth() + 1, 0, 23, 59, 59);
//use moment,js to format
var start = moment(first_day).format("YYYY-MM-DD");
var end = moment(last_day).format("YYYY-MM-DD");
Last day of the month
now = new Date
lastDayOfTheMonth = new Date(1900+now.getYear(), now.getMonth()+1, 0)
Most of these answers are missing one thing or another. After playing with most of them I came up with the following that gives you the last possible millisecond of the month.
let testDate = new Date();
console.log(getMonthEnd(testDate));
function getMonthEnd(value) {
return new Date(value.getFullYear(), value.getMonth() + 1, 0, 23, 59, 59, 999);
}
Probably missing something in this one too but seems to cover all my requirements.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
Date lastDayOfMonth = cal.getTime();
Do not forget month started with 0 so +1 in month too.
let enddayofmonth = new Date(year, month, 0).getDate();
Related
Can any one help me to find first date and last date from month and year.
I have 2 parameter month and year, for example month = 10 and year = 2021
I tried some code but it doesn't have the answer
var prevMonthLastDay = new Date(2021, 10, 0);
var thisMonthFirstDay = new Date(2021, 10, 1);
console.log(prevMonthLastDay);
console.log(thisMonthFirstDay);
Above code is giving following output:
2021-10-30T17:00:00.000Z
2021-10-31T17:00:00.000Z
My expected answer is first date 2021-10-01 and last date 2021-10-31
Try the following to get your expected output:
function GetFirstAndLastDate(year, month)
{
var firstDayOfMonth = new Date(year, month-1, 2);
var lastDayOfMonth = new Date(year, month, 1);
console.log('First Day: ' + firstDayOfMonth.toISOString().substring(0, 10));
console.log('Last Day: ' + lastDayOfMonth.toISOString().substring(0, 10));
}
GetFirstAndLastDate(2021, 10);
You can try this way
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
I'm a little bit confused with what you really want to get referencing your variable names, so I include them all:
const currentYear = 2021;
const currentMonth = 10;
const prevMonthFirstDay = new Date(currentYear, currentMonth - 2, 1);
const prevMonthLastDay = new Date(currentYear, currentMonth - 1, 0);
const currentMonthFirstDay = new Date(currentYear, currentMonth - 1, 1);
const currentMonthLastDay = new Date(currentYear, currentMonth, 0);
const nextMonthFirstDay = new Date(currentYear, currentMonth, 1);
const nextMonthLastDay = new Date(currentYear, currentMonth + 1, 0);
console.log('prev month first day:', prevMonthFirstDay);
console.log('prev month last day:', prevMonthLastDay);
console.log('current month first day:', currentMonthFirstDay);
console.log('current month last day:', currentMonthLastDay);
console.log('next month first day:', nextMonthFirstDay);
console.log('next month last day:', nextMonthLastDay);
on my system this will gives:
prev month first day: Wed Sep 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
prev month last day: Thu Sep 30 2021 00:00:00 GMT+0700 (Western Indonesia Time)
current month first day: Fri Oct 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
current month last day: Sun Oct 31 2021 00:00:00 GMT+0700 (Western Indonesia Time)
next month first day: Mon Nov 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
next month last day: Tue Nov 30 2021 00:00:00 GMT+0700 (Western Indonesia Time)
so now it will only a matter of formatting:
console.log(currentMonthFirstDay.toLocaleDateString('en-GB').split('/').reverse().join('-'));
which will give you:
2021-10-01
You can follow this.
//first and last of the current month
var current_month = "April 2017";
var arrMonth = current_month.split(" ");
var first_day = new Date(arrMonth[0] + " 1 " + arrMonth[1]);
//even though I already have the values, I'm using date functions to get year and month
//because month is zero-based
var last_day = new Date(first_day.getFullYear(), first_day.getMonth() + 1, 0, 23, 59, 59);
//use moment,js to format
var start = moment(first_day).format("YYYY-MM-DD");
var end = moment(last_day).format("YYYY-MM-DD");
the answer is in the following link:
How to get last day of the month
You can try this way referrence
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);
This month (March) has 31 days.
I want to get the last day of the month and instead of get Wed Mar 31 2021 23:59:59 I get Fri Apr 30 2021 23:59:59 look:
let d = new Date()
d.setMonth( d.getMonth() + 1) // April
d.setDate(0) // should bring the 31 of March
d.setHours(23, 59, 59, 999)
console.log(d) // Fri Apr 30 2021 23:59:59 GMT+0300 (IDT)
Why does it happen on date with 31 days?
When tried on different months every month it worked as well, for example:
let d = new Date("2021-02-25") // notice that we point to February
d.setMonth( d.getMonth() + 1)
d.setDate(0)
d.setHours(23, 59, 59, 999)
console.log(d) // Sun Feb 28 2021 23:59:59 GMT+0200 (IST)
Notice that in the second example - which is working good, we get the last day of Feb and GMT+2 (IST) and not GMT+3 (IDT)
Also notice that if I declare it like that: let d = new Date('2021-03-25') it also works good (with specific date, instead of just new Date())
It happens because April only has 30 days.
let d = new Date()
d.setMonth( d.getMonth() + 1) // Actually April 31st -> May 1st.
Try this way:
d.setMonth(d.getMonth(), 0);
second argument 0 will result in the last day of the previous month
Got it!
I set +1 for the month while the current date is 31 and what will happen is that it will jump to 31 of April which doesn't exist and the default date will be 1 in May.
So prev date of 1 in May is 30 of April.
I should set the date to 1 before doing the increment of the month, look:
let d = new Date()
d.setDate(1) // this is the change - important!
d.setMonth( d.getMonth() + 1)
d.setDate(0)
d.setHours(23, 59, 59, 999)
console.log(d) // Wed Mar 31 2021 23:59:59
That way, it will start from 1 of March, inc to 1 of April, and go prev date to last day of March.
Even that it also works, weird:
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1, 0, 0, 0, 0);
var lastDay = new Date(y, m + 1, 0, 23, 59, 59, 999)
console.log(lastDay)
I have table of records and need to get only current month records.
CODE:
let startDate = req.body.startDate
let endDate = req.body.endDate
let result = await caseRegistration.findByDate({ pathology_id : req.body.pathology_id,
created_at: {
'>=': new Date(startDate),
'<=': new Date(endDate)
}
})
Above code I am passing particular dates to get records. But my requirement is If request doesn't have any date then I want to get only current month data. Can you please help me?
var date = new Date();
var firstDay = new Date(date.getFullYear(),date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(),date.getMonth(), daysInMonth(date.getMonth()+1,
date.getFullYear()));
firstDay=>Tue Sep 01 2020 00:00:00 GMT+0530 (India Standard Time)
lastDay=> Wed Sep 30 2020 00:00:00 GMT+0530 (India Standard Time)
Please take care of timezone & date formate you want(it could be any)
If startDate and endDate are empty find first date and last date of current month by using this :
var date = new Date();
var firstDateOfCurrentMonth = new Date(date.getFullYear(), date.getMonth(), 1);
var endDateOfCurrentMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);
Now you can assign these dates to startDate and endDate
statDate=firstDateOfCurrentMonth;
endDate=endDateOfCurrentMonth;
Here's an issue I'm getting setting a date with Javascript on Chrome
"17-09-2019"
var [day, month, year] = date.split('-');
undefined
day
"17"
month
"09"
year
"2019"
var set_date = new Date(parseInt(year), parseInt(month), parseInt(day));
undefined
console.log(set_date);
VM5577:1 Thu Oct 17 2019 00:00:00 GMT+0300 (East Africa Time)
undefined
So I have a date 17-09-2019 and point here is 09 is September but when I set the date I get back October.
Been scratching my head over this one for a while now. Not sure what's going on.
The months in javascript starts from 0. You just need to subtract 1 to your parseInt(month):
var date = "17-09-2019";
var [day, month, year] = date.split('-');
console.log(`${day}-${month}-${year}`)
var set_date = new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
console.log(set_date);
Set out this , month starting from index 0
var date = "17-09-2019"
var [day, month, year] = date.split('-');
month = month - 1;
var set_date = new Date(parseInt(year), parseInt(month), parseInt(day));
console.log(set_date); //Tue Sep 17 2019 00:00:00 GMT+0530 (India Standard Time)
I don't understand why this code doesn't work. Solutions on SO mention that doing date.getDate()+1 should add a day but in my case it adds one month and two days?
var year = 2014;
var month = 3;
var day = 31;
// creating an actual date
requestedDate = new Date(year, month - 1, day);
console.debug(requestedDate.toString());
// outputs "Mon Mar 31 2014 00:00:00 GMT+0200 (CEST)"
var d = new Date();
d.setDate(requestedDate.getDate()+1);
console.debug(d.toString());
// outputs "Fri May 02 2014 11:04:52 GMT+0200 (CEST)"
You're not setting the second date to the same as the first date.
In the first new Date() you're setting the date to 31. march.
The second new Date() sets the date to today, 1. april.
31 + 1 = 32, and 1. april plus 32 days should be 2. may.
var year = 2014;
var month = 3;
var day = 31;
// creating an actual date
requestedDate = new Date(year, month - 1, day);
console.debug(requestedDate.toString());
var d = new Date(year, month - 1, day); // set the date to the same
d.setDate(requestedDate.getDate()+1);
console.debug(d.toString());
FIDDLE