1 function isToday (d: Date) {
2 const today = new Date()
4 return areDatesEqual(d, today)
7 function isYesterday (d: Date) {
8 const yesterday = new Date()
9 yesterday.setDate(yesterday.getDate() - 1)
11 return areDatesEqual(d, yesterday)
14 function isThisWeek (d: Date) {
15 const minDateOfThisWeek = new Date()
16 minDateOfThisWeek.setHours(0, 0, 0)
18 // getDay() -> Sunday - Saturday : 0 - 6
19 // We want to start our week on Monday
20 let dayOfWeek = minDateOfThisWeek.getDay() - 1
21 if (dayOfWeek < 0) dayOfWeek = 6 // Sunday
23 minDateOfThisWeek.setDate(minDateOfThisWeek.getDate() - dayOfWeek)
25 return d >= minDateOfThisWeek
28 function isThisMonth (d: Date) {
29 const thisMonth = new Date().getMonth()
31 return d.getMonth() === thisMonth
34 function isLastMonth (d: Date) {
35 const now = new Date()
37 return getDaysDifferences(now, d) <= 30
40 function isLastWeek (d: Date) {
41 const now = new Date()
43 return getDaysDifferences(now, d) <= 7
46 // ---------------------------------------------------------------------------
57 // ---------------------------------------------------------------------------
59 function areDatesEqual (d1: Date, d2: Date) {
60 return d1.getFullYear() === d2.getFullYear() &&
61 d1.getMonth() === d2.getMonth() &&
62 d1.getDate() === d2.getDate()
65 function getDaysDifferences (d1: Date, d2: Date) {
66 return (d1.getTime() - d2.getTime()) / (86400000)