84 lines
3.2 KiB
TypeScript
84 lines
3.2 KiB
TypeScript
export default class DateUtil {
|
|
public static formatNumStr(num: number) {
|
|
let str = `${num}`
|
|
if (num < 10) str = `0${num}`
|
|
return str
|
|
}
|
|
|
|
public static formateYearMonthDayStr(timestamp: number) {
|
|
const date: Date = new Date(timestamp)
|
|
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
|
|
}
|
|
|
|
public static formateMonthDayStr(timestamp: number) {
|
|
const date: Date = new Date(timestamp)
|
|
return `${date.getMonth() + 1}月${date.getDate()}日`
|
|
}
|
|
|
|
// timestamp:1453094034000 2018-1-31 19:53:44
|
|
// 根据时间戳返回 2018-1-31 19:53:44
|
|
public static formatDateStr(timestamp: number) {
|
|
const date: Date = new Date(timestamp)
|
|
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${this.formatNumStr(
|
|
date.getHours(),
|
|
)}:${this.formatNumStr(date.getMinutes())}:${this.formatNumStr(date.getSeconds())}`
|
|
}
|
|
|
|
// timestamp:1453094034000 2018-1-31-19-53-44
|
|
// 根据时间戳返回 2018-1-31-19-53-44
|
|
public static formatDateStr2(timestamp: number) {
|
|
const date: Date = new Date(timestamp)
|
|
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}-${this.formatNumStr(date.getHours())}-${this.formatNumStr(date.getMinutes())}-${this.formatNumStr(date.getSeconds())}`
|
|
}
|
|
|
|
// timestamp:1453094034000 2018-1-31
|
|
// 根据时间戳返回 2018-1-31
|
|
public static formatDateStr3(timestamp: number) {
|
|
const date: Date = new Date(timestamp)
|
|
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
|
|
}
|
|
|
|
// timestamp:1453094034000
|
|
// 根据时间戳返回 19:53
|
|
public static formatHourMinStr(timestamp: number) {
|
|
const date: Date = new Date(timestamp)
|
|
return `${this.formatNumStr(date.getHours())}:${this.formatNumStr(date.getMinutes())}`
|
|
}
|
|
|
|
// timestamp:1453094034000
|
|
// 根据时间戳返回 19:53:11
|
|
public static formatHourMinSecondStr(timestamp: number) {
|
|
const date: Date = new Date(timestamp)
|
|
return `${this.formatNumStr(date.getHours())}:${this.formatNumStr(date.getMinutes())}:${this.formatNumStr(date.getSeconds())}`
|
|
}
|
|
|
|
public static now(): number {
|
|
const date: Date = new Date()
|
|
return date.getTime()
|
|
}
|
|
|
|
public static betweenTime(startTime: number, endTime: number) {
|
|
const date: Date = new Date()
|
|
return date.getTime() >= startTime && date.getTime() <= endTime
|
|
}
|
|
|
|
// 根据时间戳返回 1天19:53:11
|
|
public static formatLeftTime(timestamp: number) {
|
|
const day: number = Math.floor(timestamp / (1000 * 60 * 60 * 24))
|
|
const hour: number = Math.floor(timestamp / (1000 * 60 * 60)) % 24
|
|
const min: number = Math.floor(timestamp / (1000 * 60)) % 60
|
|
const second: number = Math.floor(timestamp / 1000) % 60
|
|
return `${day}天${this.formatNumStr(hour)}:${this.formatNumStr(min)}:${this.formatNumStr(second)}`
|
|
}
|
|
|
|
public static isToday(dateTime: number): boolean {
|
|
const nowDate: Date = new Date()
|
|
const checkDate: Date = new Date(dateTime)
|
|
return (
|
|
checkDate.getFullYear() === nowDate.getFullYear() &&
|
|
checkDate.getMonth() === nowDate.getMonth() &&
|
|
checkDate.getDate() === nowDate.getDate()
|
|
)
|
|
}
|
|
}
|