Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert this javascript datetime object to compare to current

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?

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;

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your hive.inspection_date is returning a string, not a number; you can convert it to a Date using new Date(hive.inspection_date); then convert both date objects to a number using the +new Date(...) format →

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} Inspection date of: {new Date(inspection_date)}</Alert.Heading>

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading