Click here to Skip to main content
15,876,783 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am uncertain how to compare these two types. hive.inspection_date is returning on my console.log like 2022-06-10, when I try to compare it to DateTwo, it's not comparing as my console.log is returning (Tue May 31 2022 17:06:57 GMT-0400 (Eastern Daylight Time)). I am not sure how to convert these so they could compare and I can alert the user. Any suggestions?-REACT.js?
import React, { useState } from "react"
import Alert from 'react-bootstrap/Alert'


<pre lang="text"><pre>import React, { useState } from "react"
import Alert from 'react-bootstrap/Alert'


const  DisplayAlert= (props) => {
    const [show, setShow] = useState(true);   
 
    let dateTwo = new Date();


    
    return (
        <>
        {console.log(dateTwo)}
    {props.hives.map((hive)=> {
        if (hive.inspection_date > dateTwo){
            return
        }
        else if (hive.inspection_date  <= dateTwo, show){
            
            // if (show) {
                
                {console.log(hive.inspection_date)}
                return (
                    <Alert variant="danger" onClose={() => setShow(false)} dismissible  key={hive.id}>
                <Alert.Heading >Oh snap! You got are running behind an Inspection for Hive number: {hive.hive_number} Inspection date of: {hive.inspection_date}</Alert.Heading>

            </Alert>
            );
        // }
    }
        //   return <button onClick={() => setShow(true)}>Show Alert</button>;
        }
    )}
    </>
    )
}
 
export default DisplayAlert;


What I have tried:

I have adding getTime to the hive.inspection_date but that didn't work
Posted
Updated 31-May-22 11:47am

1 solution

import React, { useState } from "react"
import Alert from 'react-bootstrap/Alert'


const  DisplayAlert= (props) => {
    const [show, setShow] = useState(true);   
 
    let dateTwo = +new Date();
    // This needs to be converted to a `number`
    // if you want to use numeric comparison


    
    return (
        <>
        {console.log(dateTwo)}
    {props.hives.map((hive)=> {
        const inspection_date = +new Date(hive.inspection_date);
        // Generate a new Date object... Then convert it to a number

        if (inspection_date > dateTwo){
            return
        }
        else if (inspection_date  <= dateTwo, show){
            
            // if (show) {
                
                {console.log(inspection_date)}
                return (
                    <Alert variant="danger" onClose={() => setShow(false)} dismissible  key={hive.id}>
                <Alert.Heading >Oh snap! You got are running behind an Inspection for Hive number: {hive.hive_number} </Alert.Heading>

            </Alert>
            );
        // }
    }
        //   return <button onClick={() => setShow(true)}>Show Alert</button>;
        }
    )}
    </>
    )
}
 
export default DisplayAlert;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900