Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eggplantSword's avatar

Check time on date equals certain value

I have calendar events, these events can have time or not, both are saved as dateTime so when time is not being used I add 00:00:00 to the start date and 23:59:59 to the end date.

I'm using dayjs, how I can take a date and see if the time matches 00:00:00 or 23:59:59?

I'm currently playing with diff but I think there might be a better way. This works in the sense it returns 23 but I would have to add another diff for minutes and seconds to be exact. There has to be a better way to accomplish this.

dayjs(val.init_date).diff(val.end_date, 'hour')

How can I do this?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To check if a date's time matches either 00:00:00 or 23:59:59 using dayjs, you can use the format method to extract the time portion of the date and compare it to the desired values. Here's an example:

const date = dayjs('2022-01-01 00:00:00');
const time = date.format('HH:mm:ss');

if (time === '00:00:00' || time === '23:59:59') {
  console.log('Time matches');
} else {
  console.log('Time does not match');
}

In this example, we create a dayjs object from a date string that includes the time portion. We then use the format method to extract the time portion of the date in the format HH:mm:ss. We can then compare this time value to the desired values of 00:00:00 and 23:59:59 using an if statement. If the time matches either value, we log a message indicating that the time matches. Otherwise, we log a message indicating that the time does not match.

Please or to participate in this conversation.