121 lines
2.9 KiB
TypeScript
Raw Normal View History

2017-07-01 11:35:21 +00:00
import { plural } from "./Utils"
const oneSecond = 1000
const oneMinute = 60 * oneSecond
const oneHour = 60 * oneMinute
const oneDay = 24 * oneHour
const oneWeek = 7 * oneDay
const oneMonth = 30 * oneDay
const oneYear = 365.25 * oneDay
2017-06-30 15:51:17 +00:00
export var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
]
export var dayNames = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
]
2017-07-01 11:35:21 +00:00
function getRemainingTime(remaining: number): string {
let remainingAbs = Math.abs(remaining)
if(remainingAbs >= oneYear) {
return plural(Math.round(remaining / oneYear), "year")
}
if(remainingAbs >= oneMonth) {
return plural(Math.round(remaining / oneMonth), "month")
}
if(remainingAbs >= oneWeek) {
return plural(Math.round(remaining / oneWeek), "week")
}
if(remainingAbs >= oneDay) {
return plural(Math.round(remaining / oneDay), "day")
}
if(remainingAbs >= oneHour) {
return plural(Math.round(remaining / oneHour), " hour")
}
if(remainingAbs >= oneMinute) {
return plural(Math.round(remaining / oneMinute), " minute")
}
if(remainingAbs >= oneSecond) {
return plural(Math.round(remaining / oneSecond), " second")
}
return "Just now"
}
2017-07-07 15:16:40 +00:00
export function displayAiringDate(element: HTMLElement, now: Date) {
2017-07-10 18:39:50 +00:00
if(element.dataset.startDate === "") {
element.innerText = ""
return
}
2017-06-30 15:51:17 +00:00
let startDate = new Date(element.dataset.startDate)
let endDate = new Date(element.dataset.endDate)
let h = startDate.getHours()
let m = startDate.getMinutes()
let startTime = (h <= 9 ? "0" + h : h) + ":" + (m <= 9 ? "0" + m : m)
h = endDate.getHours()
m = endDate.getMinutes()
let endTime = (h <= 9 ? "0" + h : h) + ":" + (m <= 9 ? "0" + m : m)
2017-07-01 11:35:21 +00:00
let airingVerb = "will be airing"
let remaining = startDate.getTime() - now.getTime()
let remainingString = getRemainingTime(remaining)
// Add "ago" if the date is in the past
if(remainingString.startsWith("-")) {
remainingString = remainingString.substring(1) + " ago"
2017-06-30 15:51:17 +00:00
}
2017-07-01 11:35:21 +00:00
element.innerText = remainingString
2017-06-30 15:51:17 +00:00
2017-07-01 11:35:21 +00:00
if(remaining < 0) {
airingVerb = "aired"
2017-06-30 15:51:17 +00:00
}
2017-07-01 11:35:21 +00:00
element.title = "Episode " + element.dataset.episodeNumber + " " + airingVerb + " " + dayNames[startDate.getDay()] + " from " + startTime + " - " + endTime
2017-07-07 15:16:40 +00:00
}
export function displayDate(element: HTMLElement, now: Date) {
2017-07-10 18:39:50 +00:00
if(element.dataset.date === "") {
element.innerText = ""
return
}
2017-07-07 15:16:40 +00:00
let startDate = new Date(element.dataset.date)
let h = startDate.getHours()
let m = startDate.getMinutes()
let startTime = (h <= 9 ? "0" + h : h) + ":" + (m <= 9 ? "0" + m : m)
let remaining = startDate.getTime() - now.getTime()
let remainingString = getRemainingTime(remaining)
// Add "ago" if the date is in the past
if(remainingString.startsWith("-")) {
remainingString = remainingString.substring(1) + " ago"
}
element.innerText = remainingString
element.title = dayNames[startDate.getDay()] + " " + startTime
2017-06-30 15:51:17 +00:00
}