Reputation: 65
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:
Example #2 if previous year coming in last six months :
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"}
Upvotes: 1
Views: 3213
Reputation: 36
You can use moment.js and here is a fiddle to get date objects of last six months. var now = moment();
const endDate = now.format('MM-DD-YYYY')
const startDate = moment().date(1).month(now.month()).year(now.year()).format('MM-DD-YYYY')
console.log('sixth')
console.log(startDate);
console.log(endDate);
function getStartAndEndDate(month, year) {
month = month - 1;
year = month === -1 ? (year - 1) : year;
const lastDate = moment().date(1).month(month).daysInMonth();
return {
startDate: moment().date(1).month(month).year(year).format('MM-DD-YYYY'),
endDate: moment().date(lastDate).month(month).year(year).format('MM-DD-YYYY'),
date: moment().date(1).month(month).year(year),
}
}
const fifthMonth = getStartAndEndDate(now.month(), now.year());
console.log('fifth')
console.log(fifthMonth.startDate);
console.log(fifthMonth.endDate);
const fourthMonth = getStartAndEndDate(fifthMonth.date.month(), fifthMonth.date.year());
console.log('fourth')
console.log(fourthMonth.startDate);
console.log(fourthMonth.endDate);
const thirdMonth = getStartAndEndDate(fourthMonth.date.month(), fourthMonth.date.year());
console.log('third')
console.log(thirdMonth.startDate);
console.log(thirdMonth.endDate);
const secondMonth = getStartAndEndDate(thirdMonth.date.month(), thirdMonth.date.year());
console.log('second')
console.log(secondMonth.startDate);
console.log(secondMonth.endDate);
const firstMonth = getStartAndEndDate(secondMonth.date.month(), secondMonth.date.year());
console.log('firsth')
console.log(firstMonth.startDate);
console.log(firstMonth.endDate);
https://jsfiddle.net/khalid3n/yu1hbdw4/
Upvotes: 2