I have mockedData like below:
export const MOCKED_TRANSACTION_HISTORY = [
{
id: 1,
transactionDate: '01 / 03 / 2023',
transactionType: 'payment',
transactionValue: 1000,
currency: 'PLN',
},
{
id: 2,
transactionDate: '01 / 01 / 2023',
transactionType: 'withrawal',
transactionValue: 50,
currency: 'PLN',
},
{
id: 3,
transactionDate: '03 / 04 / 2023',
transactionType: 'withrawal',
transactionValue: 50,
currency: 'PLN',
},
{
id: 4,
transactionDate: '01 / 04 / 2023',
transactionType: 'withrawal',
transactionValue: 50,
currency: 'PLN',
},
{
id: 5,
transactionDate: '02 / 04 / 2023',
transactionType: 'payment',
transactionValue: 510,
currency: 'EUR',
},
{
id: 6,
transactionDate: '01 / 10 / 2021',
transactionType: 'payment',
transactionValue: 150,
currency: 'EUR',
},
{
id: 7,
transactionDate: '01 / 01 / 2019',
transactionType: 'withrawal',
transactionValue: 510,
currency: 'EUR',
},
];
My aim is to filter from mockedData elements which transactionDate is between provided startDate and endDate passed as params in useParams from react-router-dom.
Below is the output from the console, both are typeof strings
startDate: 01.03.2023;
endDate: 30.04.2023
then I change startDate and endDate to be typeof object and I create fromDate and toDate variables.
const startDateParts = startDate.split('.');
const startDateAsDate = new Date(
Number(startDateParts[2]),
Number(startDateParts[1]) - 1,
Number(startDateParts[0]),
);
const endDateParts = endDate.split('.');
const endDateAsDate = new Date(
Number(endDateParts[2]),
Number(endDateParts[1]) - 1,
Number(endDateParts[0]),
);
const fromDate = Math.min(startDateAsDate.getTime(), endDateAsDate.getTime());
const toDate = Math.max(startDateAsDate.getTime(), endDateAsDate.getTime());
For provided startDate and endDate those are fromDate and toDate:
from 1677625200000;
to 1682805600000
Can You please let me know why with below code comparison variable returns just an element with id:3 from mockedData
const [filteredData, setFilteredData] = useState<any | undefined>([]);
const data = MOCKED_TRANSACTION_HISTORY;
const comparison = data.filter((singleTransaction) => {
const date = new Date(singleTransaction.transactionDate);
console.log('transaction', date.getTime());
return date.getTime() <= toDate && date.getTime() >= fromDate;
});
setFilteredData(comparison);
console.log('comp:', comparison);
Although as You can see in mockedData there should be 4 elements ?
thanks
3
Answers
Date format used in
MOCKED_TRANSACTION_HISTORY
is different from the format used forstartDate
andendDate
. The date format inMOCKED_TRANSACTION_HISTORY
uses slashes (‘/’) while thestartDate
andendDate
use periods (‘.’). Due to this, thenew Date()
function is not correctly interpreting the date strings.Convert the
transactionDate
inMOCKED_TRANSACTION_HISTORY
to a unified format:This function will work with both formats (slashes and periods) and return a proper
Date
object.Update the
comparison
variable:The following – admittedly very short – script will get you exactly the 4 elements that you expect:
Here , I update logic and all 4 records are got , you can reefer my solution