1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| const timeStrategies = { 'A': () => { return '今天' }, 'B': (day:number|null) => { return day + '天前' }, 'C': (week:number|null) => { return week + '周前' }, 'D': () => { return "半个月前" }, 'E': (month:number|null) => { return month + "月前" }, 'F': () => { return "半年前" }, 'G': (year:number|null) => { return year + "年前" } }
class DateTransfer { nowTimeStamp:number hours:number constructor() {
this.nowTimeStamp = new Date().getTime(); this.hours = 0 } transferToTimeStamp(userTime:string) { if (userTime) { let userTimeStamp = new Date(userTime).getTime() return this.getTimeDiff(userTimeStamp) } } getTimeDiff(userTimeStamp:number):number|undefined { if (userTimeStamp && userTimeStamp < this.nowTimeStamp) { return (this.nowTimeStamp - userTimeStamp) / 1000 } } toHours(userTime:string):number|undefined { if (userTime) { let timeDiff = this.transferToTimeStamp(userTime); this.hours = Math.trunc(timeDiff as number / 3600); return this.hours }
} toDays(userTime:string) { if (userTime) { this.hours = this.toHours(userTime) as number let days = this.hours / 24 return Math.trunc(days) }
} toWeeks(userTime:string|number) { if (typeof userTime === 'string') { let days = this.toDays(userTime) as number let weeks = days / 7 return Math.trunc(weeks) } let weeks = userTime / 7 return Math.trunc(weeks)
} toMonths(userTime:string|number) { if (typeof userTime == 'string') { let days = this.toDays(userTime) as number let months = days / 30 return this.getNum(months, 0) } let months = userTime / 30 return this.getNum(months, 0) } toYears(userTime:string|number) { if (typeof userTime == 'string') { let days = this.toDays(userTime) as number let years = days / 365 return this.getNum(years, 0) } let years = userTime/365 return this.getNum(years, 0)
} getNum(num:number, cut = 3) { let numStr = num.toString() let indexEnd = numStr.indexOf(".") + cut let result = numStr if (indexEnd != -1) { result = numStr.slice(0, indexEnd) }
return Number(result) } autoTransfer(userTime:string) { if (userTime) { let days = this.toDays(userTime) as number; let index = 'A'; let value = null; if (days <= 1) { index = 'A' } else if (days > 1 && days < 7) { index = 'B' value = days } else if (days >= 7 && days < 15) { index = 'C' value = this.toWeeks(days) } else if (days >= 15 && days < 30) { index = 'D'
} else if (days >= 30 && days < 180) { index = 'E' value = this.toMonths(days) } else if (days >= 180 && days < 365) { index = 'F' } else if (days >= 365) { index = 'G' value = this.toYears(days) } return timeStrategies[index as keyof typeof timeStrategies](value) }
} }
const obj = new DateTransfer()
console.log(obj.autoTransfer('2023-01-17'))
|