PeerTube/shared/core-utils/miscs/date.ts

68 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-05-31 11:14:38 +02:00
function isToday (d: Date) {
const today = new Date()
return areDatesEqual(d, today)
}
function isYesterday (d: Date) {
const yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1)
return areDatesEqual(d, yesterday)
}
function isThisWeek (d: Date) {
const minDateOfThisWeek = new Date()
minDateOfThisWeek.setHours(0, 0, 0)
// getDay() -> Sunday - Saturday : 0 - 6
// We want to start our week on Monday
let dayOfWeek = minDateOfThisWeek.getDay() - 1
if (dayOfWeek < 0) dayOfWeek = 6 // Sunday
minDateOfThisWeek.setDate(minDateOfThisWeek.getDate() - dayOfWeek)
return d >= minDateOfThisWeek
}
function isThisMonth (d: Date) {
const thisMonth = new Date().getMonth()
return d.getMonth() === thisMonth
}
2019-06-04 10:28:19 +02:00
function isLastMonth (d: Date) {
const now = new Date()
return getDaysDifferences(now, d) <= 30
}
function isLastWeek (d: Date) {
const now = new Date()
return getDaysDifferences(now, d) <= 7
}
2019-05-31 11:14:38 +02:00
// ---------------------------------------------------------------------------
export {
isYesterday,
isThisWeek,
isThisMonth,
2019-06-04 10:28:19 +02:00
isToday,
isLastMonth,
isLastWeek
2019-05-31 11:14:38 +02:00
}
// ---------------------------------------------------------------------------
function areDatesEqual (d1: Date, d2: Date) {
return d1.getFullYear() === d2.getFullYear() &&
d1.getMonth() === d2.getMonth() &&
d1.getDate() === d2.getDate()
}
2019-06-04 10:28:19 +02:00
function getDaysDifferences (d1: Date, d2: Date) {
return (d1.getTime() - d2.getTime()) / (86400000)
}