简体   繁体   English

如何比较日期并对来自 javascript 中的 api 的数据进行排序?

[英]How do i compare dates and sort data from an api in javascript?

I am working on a project using reactjs and i would like to display data from an api based on a date selected, if it matches a date from the array of objects on the api, display the number of objects that has that date.我正在使用 reactjs 开展一个项目,我想根据所选日期显示来自 api 的数据,如果它与 Z8A5DA52ED126447D359E70C05721A 对象数组中的对象数组中的日期匹配,则该日期具有该日期。

I am able to compare just a single date so far (2020-11-07), it alerts that they are equal but not any other date, don't know what i seem to be doing wrong there.到目前为止,我只能比较一个日期(2020-11-07),它提醒它们是相等的,但不是任何其他日期,不知道我在那里似乎做错了什么。 I also have a problem displaying the number of objects with the selected date.我在显示所选日期的对象数量时也遇到了问题。

This what I have done so far这是我到目前为止所做的

import React, { useState, useEffect } from 'react';

function DateSort() {

    const [sales, displaySales] = useState([])
    const [dates, setDates] = useState()

    const value_handler = (e) => {
        let value = e.target.value;
        var timestamp = new Date(value).getTime()
        setDates(timestamp)
    }
    useEffect(() => {
        async function fetchData() {
            const response = await fetch("http://206.189.124.254:9000/sales")
            const data = await response.json()
            let length = data.length
            displaySales(data)
        }
        fetchData()
    }, [])

    return (
        <>
            <div className="form-group col">
                <input type="date" onChange={value_handler}></input>
            </div>
            {
                sales.map(sales => {
                    let date = sales.date
                    let dateobject = new Date(date)
                    console.log(dateobject)
                    if (dateobject === dates)
                        alert("yesss")
                    {

                        return (
                            <>
                                <p> {length} </p>
                            </>
                        )
                    }
                })
            }
        </>
    )
}
export default DateSort

Will appreciate some help.将不胜感激一些帮助。

I think your date comparison logic is not correct.我认为您的日期比较逻辑不正确。 This is how you can do, assuming you are not comparing the time and comparing only the dates这就是你可以做的,假设你不比较时间而只比较日期

 const sales = [ { "_id": "5fa6c32ce9a0f010733cfc02", "product_id": "5f77b78be9a0f010733cfa71", "date": 1604707200000, "unit_price": 15, "total_price": 225, "quantity": 15, "user_id": "5f83294ae9a0f010733cfb0f", "__v": 0 }, { "_id": "5fa6c390e9a0f010733cfc03", "product_id": "5f7c0ab7e9a0f010733cfa78", "date": 1604707200000, "unit_price": 14, "total_price": 196, "quantity": 14, "user_id": "5f898eb5e9a0f010733cfb91", "__v": 0 }, { "_id": "5fa6c43de9a0f010733cfc04", "product_id": "5f77b78be9a0f010733cfa71", "date": 1604707200000, "unit_price": 300, "total_price": 4200, "quantity": 14, "user_id": "5f7f992fe9a0f010733cfb06", "__v": 0 }] const date = '2020-11-07'; const matchingDates = sales.filter(sale => { const saleDate = new Date(sale.date); // ignoring the time saleDate.setHours(0, 0, 0, 0); return saleDate.getTime() === new Date(date).setHours(0,0,0,0); }) console.log(matchingDates)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM