如何转换此 javascript 日期时间对象以与当前对象进行比较
How to convert this javascript datetime object to compare to current
我不确定如何比较这两种类型。 hive.inspection_date 在我的 console.log 上返回,例如 2022-06-10,当我尝试将它与 DateTwo 进行比较时,它没有进行比较,因为我的 console.log 正在返回(2022 年 5 月 31 日星期二 17:06:57 GMT-0400(东部夏令时))。我不确定如何转换这些以便他们进行比较并且我可以提醒用户。有什么建议吗?
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;
您的hive.inspection_date
返回的是字符串,而不是数字;您可以使用 new Date(hive.inspection_date)
将其转换为 Date;然后使用 +new Date(...)
格式将 both 日期对象转换为 number →
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;
我不确定如何比较这两种类型。 hive.inspection_date 在我的 console.log 上返回,例如 2022-06-10,当我尝试将它与 DateTwo 进行比较时,它没有进行比较,因为我的 console.log 正在返回(2022 年 5 月 31 日星期二 17:06:57 GMT-0400(东部夏令时))。我不确定如何转换这些以便他们进行比较并且我可以提醒用户。有什么建议吗?
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;
您的hive.inspection_date
返回的是字符串,而不是数字;您可以使用 new Date(hive.inspection_date)
将其转换为 Date;然后使用 +new Date(...)
格式将 both 日期对象转换为 number →
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;