Home > other >  How to get current time in hh:mm:ss format using moment in React JS
How to get current time in hh:mm:ss format using moment in React JS

Time:12-17

I want to calculate the time between hours and moments. with example:

export const DataCoba: React.FC = () => {
const format = "hh:mm:ss";
  return (
    <div>
      {moment(format).isBetween(
        moment("21:00:00", format),
        moment("23:00:00", format)
      )
        ? "Between"
        : "NOOO"}
    </div>
  );
};

from the script above what I want is to get the current time, but only in hh:mm:ss format only. for example: moment('23:00:00', hh:mm:ss).

moment(format) is showing invalid date. Is there a way to do it?

CodePudding user response:

Delete your first moment's parameter. Because actually your second and third moment is have the same day as your first moment.

Try this one:

export const DataCoba: React.FC = () => {
const format = "hh:mm:ss";
  return (
    <div>
      {moment().isBetween(
        moment("21:00:00", format),
        moment("23:00:00", format)
      )
        ? "Between"
        : "NOOO"}
    </div>
  );
};

CodePudding user response:

This is my expected answer from @TimLewis

moment().isBetween(moment().set('hour', 21), moment().set('hour', 23)) ? 'Yes' : 'No'
  • Related