admin管理员组文章数量:1414613
I want to tell if a random day is in 7 days from now on. I know there is isBetween
function, but I want to solve this problem only using diff
. when I run the code below, this error occurred.
Operator '>' cannot be applied to types 'boolean' and 'number'.(2365)
Here is my code
import * as React from 'react';
import dayjs from 'dayjs';
import './style.css';
export default function App() {
const randomDay = dayjs('2022-09-15');
const today = dayjs('2022-09-12');
return (
<div>{ 0 < randomDay.diff(today, 'day') < 7 && <h1>Today is later</h1>}</div>
);
}
What is the problem? And how can I solve it?
I want to tell if a random day is in 7 days from now on. I know there is isBetween
function, but I want to solve this problem only using diff
. when I run the code below, this error occurred.
Operator '>' cannot be applied to types 'boolean' and 'number'.(2365)
Here is my code
import * as React from 'react';
import dayjs from 'dayjs';
import './style.css';
export default function App() {
const randomDay = dayjs('2022-09-15');
const today = dayjs('2022-09-12');
return (
<div>{ 0 < randomDay.diff(today, 'day') < 7 && <h1>Today is later</h1>}</div>
);
}
What is the problem? And how can I solve it?
Share Improve this question edited Sep 12, 2022 at 7:46 Nick Vu 15.5k5 gold badges28 silver badges36 bronze badges asked Sep 12, 2022 at 7:30 tehootehoo 834 silver badges9 bronze badges2 Answers
Reset to default 4You cannot do two parisions at once.
const diff = randomDay.diff(today, 'day');
...
0 < diff && diff < 7
You can do this by using the second argument in diff function.
const dayJS = require('dayjs')
const randomDay = dayJS('2022-09-15')
const today = dayJS('2022-09-12')
const difference = randomDay.diff(today, 'days')
console.log(difference) // 3 days
console.log(difference > 7 ? 'After Seven days' : 'Less than 7 days')
本文标签: javascriptHow can I get date range using dayjsStack Overflow
版权声明:本文标题:javascript - How can I get date range using dayjs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745191456a2646928.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论