I wanted to apply a filter where I can show the last six months transaction from the current date which can be any months like
Example #1 Current year:
- June 2020 (Current Month)
- May 2020
- April 2020
- March 2020
- February 2020
- January 2020
Example #2 if previous year coming in last six months :
- March 2020
- Feburary 2020
- January 2020
- December 2019
- NOvember 2019
- October 2019
so I create JS new Date object and try an approach with below mention code but don't know why getting --------- Date { NaN } in react native
Here the approach I was trying to get this working but not get success
let date = new Date()
let year = date.getFullYear()
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
//Getting last 6 months from current date
let currentMonth = date.getMonth(),
previousMonth_1 = date.getMonth() - 1,
previousMonth_2 = date.getMonth() - 2,
previousMonth_3 = date.getMonth() - 3,
previousMonth_4 = date.getMonth() - 4,
previousMonth_5 = date.getMonth() - 5
// getting total days of month
const getDaysInMonth = (month, year) => {
return new Date(year, month, 0).getDate();
};
//create function which will return the start and end object
const getDate = (month) => {
let addZeroInMonth = month < 10 ? `0${month + 1}` : month + 1
return {
startDate: new Date(`${year},${addZeroInMonth},1`),
endDate: new Date(`${year},${addZeroInMonth},${getDaysInMonth(addZeroInMonth, year)}`),
}
}
//Store each month
let m1 = getDate(currentMonth)
let m2 = getDate(previousMonth_1)
let m3 = getDate(previousMonth_2)
let m4 = getDate(previousMonth_3)
let m5 = getDate(previousMonth_4)
let m6 = getDate(previousMonth_5)
//create array for last six months to pass in API on select
let filter = [
{
label: 'Current',
"startDate": `${m1.startDate} 00:00:00`,
"endDate": `${m1.endDate} 00:00:00`,
},
{
label: months[previousMonth_1] + ' ' + year,
"startDate": `${m2.startDate} 00:00:00`,
"endDate": `${m2.endDate} 00:00:00`,
},
{
label: months[previousMonth_2] + ' ' + year,
"startDate": `${m3.startDate} 00:00:00`,
"endDate": `${m3.endDate} 00:00:00`,
},
{
label: months[previousMonth_3] + ' ' + year,
"startDate": `${m4.startDate} 00:00:00`,
"endDate": `${m4.endDate} 00:00:00`,
},
{
label: months[previousMonth_4] + ' ' + year,
"startDate": `${m5.startDate} 00:00:00`,
"endDate": `${m5.endDate} 00:00:00`,
},
{
label: months[previousMonth_5] + ' ' + year,
"startDate": `${m6.startDate} 00:00:00`,
"endDate": `${m6.endDate} 00:00:00`,
},
]
unfortunately getting this output in react native
{"endDate": Date { NaN }, "startDate": Date { NaN }}
{"endDate": Date { NaN }, "startDate": Date { NaN }}
but if I run this code in browser its working fine and I am also able to extract the year, month, day from it
Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time)
Fri Jan 31 2020 00:00:00 GMT+0530 (India Standard Time)
Need Something like this to send data in API
LOG {"endDate": "2020-06-30 00:00:00", "label": "Current", "startDate": "2020-06-01 00:00:00"}
LOG {"endDate": "2020-05-31 00:00:00", "label": "May 2020", "startDate": "2020-05-01 00:00:00"}
LOG {"endDate": "2020-04-30 00:00:00", "label": "April 2020", "startDate": "2020-04-01 00:00:00"}
LOG {"endDate": "2020-03-31 00:00:00", "label": "March 2020", "startDate": "2020-03-01 00:00:00"}
LOG {"endDate": "2020-02-29 00:00:00", "label": "February 2020", "startDate": "2020-02-01 00:00:00"}
LOG {"endDate": "2020-01-31 00:00:00", "label": "January 2020", "startDate": "2020-01-01 00:00:00"}
