#check date range

8 messages · Page 1 of 1 (latest)

dense dove
#

What are your current thoughts on how you might solve this? What have you already tried? Should we assume this is in JavaScript? Have you looked at the JavaScript language reference for the Date() object? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects encapsulate an integral number that represents milliseconds since the midnight at the beginning of January 1, 1970, UTC (the epoch).

orchid axle
#

this is in js

#
import { useState, useEffect } from "react";
import useGlobalContext from "../Context/context";

export default function CheckDate() {
  const { horoscopeData, setIsFalls } = useGlobalContext();
  const [date, setDate] = useState(new Date(horoscopeData.current_date));
  const dateRange = "Jan 20 - Feb 18";
  const [startStr, endStr] = dateRange.split(" - ");
  const startMonth = new Date(startStr).getMonth();
  const endMonth = new Date(endStr).getMonth();
  const startDate = new Date(startStr).getDate();
  const endDate = new Date(endStr).getDate();

  const currentMonth = date.getMonth();
  const currentDate = date.getDate();

  const isWithinRange =
    (currentMonth > startMonth ||
      (currentMonth === startMonth && currentDate >= startDate)) &&
    (currentMonth < endMonth ||
      (currentMonth === endMonth && currentDate <= endDate));

      console.log(isWithinRange)
  useEffect(() => {
    if (isWithinRange) {
      setIsFalls(true);
    }else {
      setIsFalls(false)
    }
  }, [isWithinRange]);
}
#

i did this but it is showing true all the time

#

its working now, thank you so much

#

i have an question, thought.

#

can i ask that its on TS

#

i though its working but its not😭