@dingdaoos/lucid-utils
Version:
Lucid utils
102 lines (93 loc) • 2.82 kB
text/typescript
import { padZero } from './tools'
export class TimeInfo {
options: any
noon: string[]
hours: any[]
minutes: any[]
seconds: any[]
timeList: any[]
constructor(options = { hourType: 24, hourDuration: '0-23', minuteDuration: '0-59', secondDuration: '0-59', interval: 1 }) {
this.setOptions(options)
this.getHour()
this.getMinute()
this.getSecond()
this.getTimeList(options.interval)
}
// set options
setOptions(options) {
this.options = options
}
// get hour function
getHour() {
const hourType = this.options.hourType
const hourDuration = this.options.hourDuration ? this.options.hourDuration : hourType === 24 ? '0-23' : '0-11'
const duration = hourDuration.split('-')
const start = parseInt(duration[0])
const end = parseInt(duration[1])
const hours = []
for (let i = start; i <= end; i += 1) {
hours.push(padZero(i))
}
if (hourType === 12) {
this.noon = ['AM', 'PM']
}
this.hours = hours
}
// get minute funcgtion
getMinute() {
const minuteDuration = this.options.minuteDuration
const duration = minuteDuration.split('-')
const start = parseInt(duration[0])
const end = parseInt(duration[1])
const minutes = []
if (end >= 60) {
for (let i = start; i < 60; i += 1) {
minutes.push(padZero(i))
}
} else {
for (let i = start; i <= end; i += 1) {
minutes.push(padZero(i))
}
}
this.minutes = minutes
}
// get second function
getSecond() {
const secondDuration = this.options.secondDuration
const duration = secondDuration.split('-')
const start = parseInt(duration[0])
const end = parseInt(duration[1])
const seconds = []
if (end >= 60) {
for (let i = start; i < 60; i += 1) {
seconds.push(padZero(i))
}
} else {
for (let i = start; i <= end; i += 1) {
seconds.push(padZero(i))
}
}
this.seconds = seconds
}
getTimeList(interval) {
let size = Math.floor(24 * 60 / interval)
const time = new Array(size).fill('').map((item, index) => {
let startVal = index * interval
let startHour = Math.floor((startVal / 60))
let startMinute = (startVal % 60)
let startTime = ((startHour < 10) ? ('0' + startHour) : startHour) + ':' + (startMinute < 10 ? '0' + startMinute : startMinute)
return startTime
})
this.timeList = time
}
// get nowinfo function
getNowInfo() {
const nowInfo = new Date()
const dateInfo = padZero(nowInfo.getFullYear()) + '-' + padZero(nowInfo.getMonth() + 1) + '-' + padZero(nowInfo.getDate())
const timeInfo = padZero(nowInfo.getHours()) + ':' + padZero(nowInfo.getMinutes()) + ':' + padZero(nowInfo.getSeconds())
return {
dateInfo,
timeInfo
}
}
}