1

I have a two sets of data in database one is date and one is time. I display my data in calendar. I made one post request when user choose the date, that time will be selected for the user. I want to make one helper function when user's choosing time will be over I want to show them alert in front-end that "Your selected time expired!". I am using date-fns for date validation.

This is my code so far:

const { isToday } = require("date-fns");
   
const helperFunction = (date, time) => {
  if (isToday(new Date(date))) {
       
    // in here I want to compare to current time and selected time ("15:30-16:30")
  }
};

console.log(helperFunction("2021-06-15", "15:30-16:30"));
Krisna
  • 2,854
  • 2
  • 24
  • 66
  • Your code so far doesn't show a real attempt, or explain what you are having difficulty with. You can get the current time from `new Date()`, and you can [compare dates using `Date.prototype.getTime()`](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – Ruan Mendes Jun 15 '21 at 13:15
  • You have Really good point, sometimes I am so rush that I forget to mention everything . I am struggling this time format "15:30-16:30", how can I compare current time and trying to make efficient code. – Krisna Jun 15 '21 at 13:19

1 Answers1

2

Have a go with this

  1. No need for date-fns for trivial comparison
  2. I do string comparison, it works on same length strings. No need to create new dates for the time

I am not sure where you will pass the user time

const isToday = d => { const d1 = new Date(); return d.getFullYear() === d1.getFullYear() && d.getMonth() === d1.getMonth() && d.getDate() === d1.getDate() }
   
const helperFunction = (date, time) => {
  const [yyyy,mm,dd] = date.split("-");
  let d = new Date(yyyy,mm-1,dd);
  if (isToday(d)) {
    const hhmm =  d.toTimeString().match(/(\d{2}:\d{2}):.*/)[1]
    const range = time.split("-")
    return hhmm >= range[0] && hhmm <= range[1]
  }
  return false
};

console.log(helperFunction("2021-06-15", "15:30-18:30"));
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • What help do you need with date-fns? – mplungjan Jun 15 '21 at 13:33
  • Ok so use it. It was quicker for me to write a function than to find and copy the cdn – mplungjan Jun 15 '21 at 13:37
  • Ah, I assumed your original code using date-fns worked for you – mplungjan Jun 15 '21 at 13:39
  • so far yes. if today is true then compare the selected time and current time where I stuck – Krisna Jun 15 '21 at 13:41
  • Just replace my `const isToday` function with `const { isToday } = require("date-fns");` as you have it – mplungjan Jun 15 '21 at 13:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233798/discussion-between-krisna-and-mplungjan). – Krisna Jun 15 '21 at 13:49
  • Sorry. my mistake – Krisna Jun 16 '21 at 09:19
  • Creating a date then setting the parts one by one is fraught as if the original date is say 31 July and you set the month to June, then it will roll over to 1 July and when you lastly set the date it will be in the wrong month. Besides, it's much more efficient to do `let d = new Date(yyyy, mm-1, dd)`. :-) – RobG Jun 16 '21 at 09:37
  • @RobG Yeah, it came about because OP wants to check a TIME and new Date would create a different time. I have updated the answer – mplungjan Jun 16 '21 at 09:49