maccounter
45 строк · 1.0 Кб
1import { isBetween } from '@utils/isBetween'2
3export const getCurrentTimePeriod = () => {4const periods = {5night: {6min: 0,7max: 5,8},9morning: {10min: 6,11max: 11,12},13afternoon: {14min: 12,15max: 17,16},17evening: {18min: 18,19max: 23,20},21}22
23let currentHour = new Date()24.toLocaleTimeString('en-US', { hour12: false })25.split(':')[0] as unknown as number26
27if (Number(currentHour) === 24) {28currentHour = 029}30
31const isNight = isBetween({ value: currentHour, ...periods.night })32const isEvening = isBetween({ value: currentHour, ...periods.evening })33const isAfternoon = isBetween({ value: currentHour, ...periods.afternoon })34
35switch (true) {36case isNight:37return 'night'38case isEvening:39return 'evening'40case isAfternoon:41return 'afternoon'42default:43return 'morning'44}45}
46