gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
in angular, given two dates, how can i test that, without looking at the hours, they are…
- User
in angular, given two dates, how can i test that, without looking at the hours, they are pointing at the same day in the calendar ?
- Assistant
In Angular (or in plain TypeScript/JavaScript), if you want to check whether two
Dateobjects correspond to the same calendar day regardless of their time components (hours, minutes, seconds, milliseconds), you need to compare their year, month, and date parts only.Here's a simple way to do that:
function isSameDay(date1: Date, date2: Date): boolean { return date1.getFullYear() === date2.getFullYear() && date1.getMonth() === date2.getMonth() && date1.getDate() === date2.getDate(); }Example usage:
const d1 = new Date('2024-06-26T10:30:00'); const d2 = new Date('2024-06-26T23:59:59'); const d3 = new Date('2024-06-27T00:00:00'); console.log(isSameDay(d1, d2)); // true console.log(isSameDay(d1, d3)); // falseExplanation:
getFullYear()returns the year.getMonth()returns the month index (0-based, so January is 0).