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 // ---------------------------------------------------------------------------
48 function timeToInt (time: number | string) {
50 if (typeof time === 'number') return time
52 const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
53 const matches = time.match(reg)
55 if (!matches) return 0
57 const hours = parseInt(matches[2] || '0', 10)
58 const minutes = parseInt(matches[4] || '0', 10)
59 const seconds = parseInt(matches[6] || '0', 10)
61 return hours * 3600 + minutes * 60 + seconds
64 function secondsToTime (seconds: number, full = false, symbol?: string) {
67 if (seconds === 0 && !full) return '0s'
69 const hourSymbol = (symbol || 'h')
70 const minuteSymbol = (symbol || 'm')
71 const secondsSymbol = full ? '' : 's'
73 const hours = Math.floor(seconds / 3600)
74 if (hours >= 1) time = hours + hourSymbol
75 else if (full) time = '0' + hourSymbol
78 const minutes = Math.floor(seconds / 60)
79 if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol
80 else if (minutes >= 1) time += minutes + minuteSymbol
81 else if (full) time += '00' + minuteSymbol
84 if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol
85 else if (seconds >= 1) time += seconds + secondsSymbol
86 else if (full) time += '00'
91 // ---------------------------------------------------------------------------
104 // ---------------------------------------------------------------------------
106 function areDatesEqual (d1: Date, d2: Date) {
107 return d1.getFullYear() === d2.getFullYear() &&
108 d1.getMonth() === d2.getMonth() &&
109 d1.getDate() === d2.getDate()
112 function getDaysDifferences (d1: Date, d2: Date) {
113 return (d1.getTime() - d2.getTime()) / (86400000)