You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.3 KiB
JavaScript

export function getHoursBetween(startHourStr, endHourStr) {
const startHour = new Date(startHourStr)
let endHour = new Date(endHourStr)
let nowDate = new Date()
nowDate.setHours(nowDate.getHours() - 1);
if (endHour.getTime() > nowDate.getTime()) {
endHour = nowDate
}
const hours = []
while (startHour <= endHour) {
const hourString = `${startHour.getFullYear()}-${String(startHour.getMonth() + 1).padStart(2, '0')}-${String(startHour.getDate()).padStart(2, '0')} ${String(startHour.getHours()).padStart(2, '0')}:00:00`
hours.push(hourString)
startHour.setTime(startHour.getTime() + 60 * 60 * 1000)
}
// return hours;
return hours.sort((a, b) => new Date(b) - new Date(a))
}
export function getDatesBetween(startDateStr, endDateStr) {
const startDate = new Date(startDateStr)
let endDate = new Date(endDateStr)
let nowDate = new Date()
nowDate.setHours(nowDate.getHours() - 1);
if (endDate.getTime() > nowDate.getTime()) {
endDate = nowDate
}
const dates = []
while (startDate <= endDate) {
dates.push(`${startDate.getFullYear()}-${String(startDate.getMonth() + 1).padStart(2, '0')}-${String(startDate.getDate()).padStart(2, '0')}`)
startDate.setDate(startDate.getDate() + 1)
}
// return dates;
return dates.sort((a, b) => new Date(b) - new Date(a))
}
export function getMonthsBetween(startMonthStr, endMonthStr) {
const result = []
const startDate = new Date(startMonthStr + '-01')
let endDate = new Date(endMonthStr + '-01')
const currentDate = new Date(startDate)
let nowDate = new Date()
nowDate.setHours(nowDate.getHours() - 1);
if (endDate.getTime() > nowDate.getTime()) {
endDate = nowDate
}
while (currentDate <= endDate) {
const year = currentDate.getFullYear()
const month = String(currentDate.getMonth() + 1).padStart(2, '0')
result.push(`${year}-${month}`)
currentDate.setMonth(currentDate.getMonth() + 1)
}
return result.sort((a, b) => new Date(b) - new Date(a))
}
export function getYearsBetween(startYearStr, endYearStr) {
const result = []
const startYear = Number(startYearStr.substring(0, 4))
const endYear = Number(endYearStr.substring(0, 4))
for (let i = startYear; i <= endYear; i++) {
result.push(`${i}`)
}
return result.sort((a, b) => new Date(b) - new Date(a))
}