最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I get date range using dayjs? - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

You 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')
发布评论

评论列表(0)

  1. 暂无评论