I need to fetch the first and last day of the current month. I created the solution using the core JS new Date() method.
const date = new Date();
const firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
const lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
console.log(firstDay.toUTCString(), lastDay.toUTCString()); //Tue, 31 Mar 2020 18:30:00 GMT Wed, 29 Apr 2020 18:30:00 GMT
When I try a similar thing in the browser's console, it prints the result as expected, i.e. 1st April 2020 and 30th April 2020, but testing it in the postman environment, gives the wrong result.
Can anyone please help to resolve this confusion?
Your code:
var d = new Date(),
e = new Date(d.getFullYear(), d.getMonth(), 1),
f = new Date(d.getFullYear(), d.getMonth() + 1, 0);
console.log(e.toUTCString() + "\n" + f.toUTCString());
so you get wrong result because you want to get the UTC string
from local date, you need to make it UTC date in order to print
UTC string correctly.
var d = new Date(),
e = new Date(Date.UTC(d.getFullYear(), d.getMonth(), 1)),
f = new Date(Date.UTC(d.getFullYear(), d.getMonth() + 1, 0));
console.log(e.toUTCString() + "\n" + f.toUTCString());
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);
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;
I created a function that produces a loop of days in month.
Here's my code
var date = new Date()
var firstDay = new Date(date.getFullYear(), date.getMonth(), 2);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 1);
firstDay.setHours(0, 0, 0, 0)
lastDay.setHours(0, 0, 0, 0)
console.log('Today date: ', date)
console.log('First Day: ', firstDay)
console.log('Last Day: ', lastDay)
Why the First Day and Last Day starts at 17:00 ?
you can 'force' the dates to start at zero dark hundred by doing this:
const date = new Date()
date.setHours(0,0,0,0);
I suspect that your requirements might be more complex. If that is the case, you can force the user's browser/device to adopt a specific timezone, such as this:
date.toLocaleString('en-GB', { timeZone: 'UTC' });
And since you mentioned you are running it on Node.js which has a different timezone configuration,
const today = new Date(new Date().setUTCHours(0,0,0,0));
const todayISO = today.toISOString();
console.log(todayISO);
new Date()
This returns an object which represent the current time of the system where the command runs.
Thu Feb 28 2019 15:14:30 GMT+0530 (India Standard Time)
I have referred to the link javascript getTime() to 10 digits only. And I got to know the way to convert date to 10 digit timestamp. My requirement is that I want last month date timestamp. Below is the code that I have tried:
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
var monthLastDay = Math.floor(lastDay.getTime()/1000);
console.log(monthLastDay);
When I run above code I get output: 1527705000. And when I check the output on https://www.unixtimestamp.com/index.php. I got
1527705000
Is equivalent to:
05/30/2018 # 6:30pm (UTC)
Which I think is not correct as the month 05 has 31 days. So I should timestamp as 05/31/2018.
EDIT:
If I try with below code:
var date = new Date('6/8/2018');
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
var monthLastDay = Math.floor(lastDay.getTime()/1000);
console.log(monthLastDay);
I get output: 1530297000 which is equal to 6/29/2018. But output should be 6/30/2018.
Let me know possible solution to get the correct 10 digit timestamp.
I guess the problem is with your timezone.
> new Date('6/8/2018')
Fri Jun 08 2018 00:00:00 GMT+0200 (CEST)
> new Date('6/8/2018').getTime()
1528408800000 // which is 06/07/2018 # 10:00pm (UTC)
You should build the date in UTC if you want to use the timestamp as is
> var date = new Date('6/8/2018');
> Date.UTC(date.getFullYear(), date.getMonth() + 1, 0);
1530316800000 // which is 06/30/2018 # 12:00am (UTC)
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();