Last active
September 16, 2024 11:36
-
-
Save andrejsharapov/e665ef0ddb38be8994d069b13842ea4b to your computer and use it in GitHub Desktop.
таймер обратного отсчета на js или moment.js с таймзоной
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import moment from 'moment'; | |
| eventTimer() { | |
| // const now = new Date(); | |
| // const start = new Date(this.info.recdatebegin); | |
| // const end = new Date(this.info.recdateend); | |
| // const diff = new Date(start.getTime() - now.getTime()); | |
| // const empty = new Date(0); | |
| // const days = diff.getDate() - empty.getDate(); | |
| // const hours = diff.getHours() - empty.getHours(); | |
| // const min = diff.getMinutes() - empty.getMinutes(); | |
| // const sec = diff.getSeconds() - empty.getSeconds(); | |
| const now = moment().tz('Europe/Moscow').unix(); | |
| const start = moment(this.info.recdatebegin).tz('Europe/Moscow').unix(); | |
| const end = moment(this.info.recdateend).tz('Europe/Moscow').unix(); | |
| const diff = start - now; | |
| const duration = moment.duration(diff * 1000, 'milliseconds'); | |
| const days = duration.days(); | |
| const hours = duration.hours(); | |
| const min = duration.minutes(); | |
| const sec = duration.seconds(); | |
| this.quiz.message = 'До начала события осталось:'; | |
| this.quiz.timer = ` | |
| ${this.getNoun(days <= 0 ? 0 : days, 'день', 'дня', 'дней')} | |
| ${this.getNoun(hours <= 0 ? 0 : hours, 'час', 'часа', 'часов')} | |
| ${this.getNoun(min <= 0 ? 0 : min, 'минута', 'минуты', 'минут')} | |
| ${this.getNoun(sec <= 0 ? 0 : sec, 'секунда', 'секунды', 'секунд')} | |
| `; | |
| // снимаем дизейбл с кнопки для доступа в модалку за 30 мин до начала и на момент события | |
| const subtract = moment(this.info.recdatebegin) | |
| .subtract(30, 'm') | |
| .tz('Europe/Moscow') | |
| .unix(); | |
| if (now <= end && now >= subtract) { | |
| this.quiz.entryBtnDisabled = false; | |
| } | |
| if (now >= start && now < end) { | |
| this.quiz.message = 'Событие уже началось!'; | |
| this.quiz.eventBtnDisabled = false; | |
| this.quiz.timer = null; | |
| } | |
| if (now >= end) { | |
| this.quiz.message = 'Событие завершено.'; | |
| this.quiz.eventBtnDisabled = true; | |
| this.quiz.entryBtnDisabled = false; | |
| this.quiz.entryBtnText = 'Смотреть результаты'; | |
| this.quiz.timer = null; | |
| } | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| getNoun(number, one, two, five) { | |
| let n = Math.abs(number); | |
| n %= 100; | |
| function name(n) { | |
| if (n >= 5 && n <= 20) { | |
| return five; | |
| } | |
| n %= 10; | |
| if (n === 1) { | |
| return one; | |
| } | |
| if (n >= 2 && n <= 4) { | |
| return two; | |
| } | |
| return five; | |
| } | |
| return number + ' ' + name(n); | |
| }, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment