Search code examples
javascriptreactjstypescriptdayjs

How can I get date range using dayjs?


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?


Solution

  • You cannot do two comparisions at once.

    const diff = randomDay.diff(today, 'day');
    

    ...

    0 <  diff && diff < 7