time_format.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. export class TimeFormatter {
  2. static nowDuration = 30
  3. static convertGMTtoOffset(gmtString) {
  4. // 提取符号、小时和分钟
  5. var sign = gmtString.substring(3, 4); // 提取符号 "+" 或 "-"
  6. var hours = parseInt(gmtString.substring(4, 6)); // 提取小时部分
  7. var minutes = parseInt(gmtString.substring(6, 8)); // 提取分钟部分
  8. // 计算偏移量(以分钟为单位)
  9. var offset = (hours * 60 + minutes) * (sign === '+' ? 1 : -1);
  10. return offset;
  11. }
  12. //把本地时间戳更改为指定时区的时间戳
  13. static transferTimestamp(timestamp: number, timeZone: string) {
  14. if (!timeZone || timeZone.length == 0) {
  15. return timestamp
  16. }
  17. var date = new Date(timestamp)
  18. if (timeZone) {
  19. var localOffset = date.getTimezoneOffset()
  20. var targetOffset = localOffset + TimeFormatter.convertGMTtoOffset(timeZone)
  21. // 计算目标时间值(毫秒数加上偏移量的毫秒数)
  22. var targetTime = date.getTime() + (targetOffset * 60 * 1000);
  23. // 创建目标时区的 Date 对象
  24. var targetDate = new Date(targetTime);
  25. return targetDate.getTime()
  26. }
  27. return timestamp
  28. }
  29. //格式化时间
  30. static formatTimestamp(timestamp: number): string {
  31. const currentDate = new Date();
  32. const inputDate = new Date(timestamp);
  33. // 判断是否是今天
  34. if (
  35. inputDate.getDate() === currentDate.getDate() &&
  36. inputDate.getMonth() === currentDate.getMonth() &&
  37. inputDate.getFullYear() === currentDate.getFullYear()
  38. ) {
  39. return `${TimeFormatter.getTodayUnit()} ${TimeFormatter.formatTime(inputDate)}`;
  40. }
  41. // 判断是否是昨天
  42. const yesterday = new Date();
  43. yesterday.setDate(currentDate.getDate() - 1);
  44. if (
  45. inputDate.getDate() === yesterday.getDate() &&
  46. inputDate.getMonth() === yesterday.getMonth() &&
  47. inputDate.getFullYear() === yesterday.getFullYear()
  48. ) {
  49. return `${TimeFormatter.getYesterdayUnit()} ${TimeFormatter.formatTime(inputDate)}`;
  50. }
  51. // 判断是否是明天
  52. const tomorrow = new Date();
  53. tomorrow.setDate(currentDate.getDate() + 1);
  54. if (
  55. inputDate.getDate() === tomorrow.getDate() &&
  56. inputDate.getMonth() === tomorrow.getMonth() &&
  57. inputDate.getFullYear() === tomorrow.getFullYear()
  58. ) {
  59. return `${TimeFormatter.getTomorrowUnit()} ${TimeFormatter.formatTime(inputDate)}`;
  60. }
  61. // 返回 YYYY-MM-DD HH:mm
  62. return `${inputDate.getFullYear()}-${TimeFormatter.formatNumber(inputDate.getMonth() + 1)}-${TimeFormatter.formatNumber(
  63. inputDate.getDate()
  64. )} ${TimeFormatter.formatTime(inputDate)}`;
  65. }
  66. static dateTimeFormate(timestamp: number) {
  67. const currentDate = new Date();
  68. const inputDate = new Date(timestamp);
  69. // 判断是否是今天
  70. if (
  71. inputDate.getDate() === currentDate.getDate() &&
  72. inputDate.getMonth() === currentDate.getMonth() &&
  73. inputDate.getFullYear() === currentDate.getFullYear()
  74. ) {
  75. // if (global.language == 'en') {
  76. // return `${TimeFormatter.padZero(inputDate.getHours())}:${TimeFormatter.padZero(inputDate.getMinutes())} ${TimeFormatter.getTodayUnit()}`
  77. // }
  78. return `${TimeFormatter.getTodayUnit()} ${TimeFormatter.padZero(inputDate.getHours())}:${TimeFormatter.padZero(inputDate.getMinutes())}`;
  79. }
  80. // 判断是否是昨天
  81. const yesterday = new Date();
  82. yesterday.setDate(currentDate.getDate() - 1);
  83. if (
  84. inputDate.getDate() === yesterday.getDate() &&
  85. inputDate.getMonth() === yesterday.getMonth() &&
  86. inputDate.getFullYear() === yesterday.getFullYear()
  87. ) {
  88. // if (global.language == 'en') {
  89. // return `${TimeFormatter.padZero(inputDate.getHours())}:${TimeFormatter.padZero(inputDate.getMinutes())} ${TimeFormatter.getYesterdayUnit()}`;
  90. // }
  91. return `${TimeFormatter.getYesterdayUnit()} ${TimeFormatter.padZero(inputDate.getHours())}:${TimeFormatter.padZero(inputDate.getMinutes())}`;
  92. }
  93. // if (global.language == 'en') {
  94. // return `${TimeFormatter.padZero(inputDate.getHours())}:${TimeFormatter.padZero(inputDate.getMinutes())} ${TimeFormatter.getDateAndWeek(timestamp)}`
  95. // }
  96. return TimeFormatter.getDateAndWeek(timestamp) + ' ' + `${TimeFormatter.padZero(inputDate.getHours())}:${TimeFormatter.padZero(inputDate.getMinutes())}`
  97. }
  98. //显示完整日期
  99. static timelineFullFormatTime(timestamp: number): string {
  100. var date = new Date(timestamp)
  101. var str = `${TimeFormatter.formatNumber(date.getHours())}:${TimeFormatter.formatNumber(date.getMinutes())}`;
  102. if (global.language == 'en') {
  103. return `${TimeFormatter.getDayOfWeek(date.getDay())} ${TimeFormatter.getMonth(date.getMonth() + 1)} ${date.getDate()} ${str}`
  104. }
  105. return `${TimeFormatter.getMonth(date.getMonth() + 1)}${date.getDate()}日 ${TimeFormatter.getDayOfWeek(date.getDay())} ${str}`
  106. }
  107. //只显示时间
  108. static timelineFormatTime(timestamp: number, isTestUser?: boolean): string {
  109. if (!timestamp) {
  110. return ' '
  111. }
  112. var date = new Date(timestamp)
  113. // 返回 HH:mm
  114. var str = `${TimeFormatter.formatNumber(date.getHours())}:${TimeFormatter.formatNumber(date.getMinutes())}`;
  115. if (isTestUser) {
  116. str += `:${TimeFormatter.formatNumber(date.getSeconds())}`
  117. }
  118. return str
  119. }
  120. //2.基于日期显示日期,省略今天描述
  121. static dateDescription(timestamp: number, showToday?: boolean, showFullDate?: boolean): string {
  122. if (!timestamp) {
  123. return ''
  124. }
  125. const currentDate = new Date();
  126. const inputDate = new Date(timestamp);
  127. // 判断是否是今天
  128. if (
  129. inputDate.getDate() === currentDate.getDate() &&
  130. inputDate.getMonth() === currentDate.getMonth() &&
  131. inputDate.getFullYear() === currentDate.getFullYear() &&
  132. !showFullDate
  133. ) {
  134. if (currentDate.getTime() - timestamp <= TimeFormatter.nowDuration * 1000 && currentDate.getTime() - timestamp >= 0) {
  135. return TimeFormatter.getJustNowUnit()
  136. }
  137. return showToday ? TimeFormatter.getTodayUnit() : ``;
  138. }
  139. // 判断是否是昨天
  140. const yesterday = new Date();
  141. yesterday.setDate(currentDate.getDate() - 1);
  142. if (
  143. inputDate.getDate() === yesterday.getDate() &&
  144. inputDate.getMonth() === yesterday.getMonth() &&
  145. inputDate.getFullYear() === yesterday.getFullYear() &&
  146. !showFullDate
  147. ) {
  148. return TimeFormatter.getYesterdayUnit();
  149. }
  150. // 判断是否是明天
  151. const tomorrow = new Date();
  152. tomorrow.setDate(currentDate.getDate() + 1);
  153. if (
  154. inputDate.getDate() === tomorrow.getDate() &&
  155. inputDate.getMonth() === tomorrow.getMonth() &&
  156. inputDate.getFullYear() === tomorrow.getFullYear() &&
  157. !showFullDate
  158. ) {
  159. return TimeFormatter.getTomorrowUnit();
  160. }
  161. const utcDate1 = Date.UTC(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate())
  162. const utcDate2 = Date.UTC(inputDate.getFullYear(), inputDate.getMonth(), inputDate.getDate())
  163. const oneDayMilliseconds = 24 * 60 * 60 * 1000; // 一天的毫秒数
  164. var dayDifference = Math.floor((utcDate2 - utcDate1) / oneDayMilliseconds);
  165. if (dayDifference < 0) {
  166. var strMonth = TimeFormatter.getMonth(inputDate.getMonth() + 1)
  167. var strDt = ''
  168. var strYear = ''
  169. if (currentDate.getFullYear() != inputDate.getFullYear()) {
  170. strYear = inputDate.getFullYear() + ''
  171. strDt = strYear + '年'
  172. }
  173. if (global.language == 'en') {
  174. if (strYear.length > 0) {
  175. return strMonth + ' ' + inputDate.getDate() + ',' + strYear
  176. }
  177. return strMonth + ' ' + inputDate.getDate()
  178. }
  179. strDt = strDt + strMonth
  180. strDt = strDt + (inputDate.getDate()) + '日'
  181. return strDt
  182. }
  183. return `${dayDifference}天后`
  184. // return `${inputDate.getFullYear()}-${TimeFormatter.formatNumber(inputDate.getMonth() + 1)}-${TimeFormatter.formatNumber(
  185. // inputDate.getDate()
  186. // )}`;
  187. }
  188. //3. datedescription+时间,刚刚时隐藏时间
  189. static datetimeDescription(timestamp: number): string {
  190. const currentDate = new Date();
  191. if (currentDate.getTime() - timestamp <= TimeFormatter.nowDuration * 1000) {
  192. return TimeFormatter.dateDescription(timestamp, true)
  193. }
  194. if (global.language == 'en') {
  195. return TimeFormatter.dateDescription(timestamp, true) + ' ' + TimeFormatter.timeDescription(timestamp)
  196. }
  197. return TimeFormatter.dateDescription(timestamp, true) + '' + TimeFormatter.timeDescription(timestamp)
  198. }
  199. static timeDescription(timestamp): string {
  200. const date = new Date(timestamp);
  201. return `${TimeFormatter.padZero(date.getHours())}:${TimeFormatter.padZero(date.getMinutes())}`;
  202. }
  203. static formatTime(date: Date): string {
  204. return `${TimeFormatter.formatNumber(date.getHours())}:${TimeFormatter.formatNumber(date.getMinutes())}:${TimeFormatter.formatNumber(date.getSeconds())}`;
  205. }
  206. static formatNumber(num: number): string {
  207. return num.toString().padStart(2, '0');
  208. }
  209. static getYearByDate(num: number): string {
  210. if (global.language == 'en') {
  211. return (num + '').substring(0, 4)
  212. }
  213. return (num + '').substring(0, 4) + '年'
  214. }
  215. static getMonthAndDayByDate(num: number): string {
  216. const dt = new Date((num + '').substring(0, 4) + '/' +
  217. (num + '').substring(4, 6) + '/' +
  218. (num + '').substring(6));
  219. const now = new Date();
  220. const diff = now.getTime() - dt.getTime();
  221. const day = 1000 * 60 * 60 * 24;
  222. if (diff < day) {
  223. return TimeFormatter.getTodayUnit();
  224. } else if (diff < 2 * day) {
  225. return TimeFormatter.getYesterdayUnit();
  226. } else {
  227. var month = parseInt((num + '').substring(4, 6))
  228. var date = parseInt((num + '').substring(6, 8))
  229. if (global.language == 'en') {
  230. return TimeFormatter.getMonth(month) + ' ' + date
  231. }
  232. return TimeFormatter.getMonth(month) + date + '日'
  233. }
  234. }
  235. static getMonthAndDayByTimestamp(num: number, showFullDate?: boolean): string {
  236. const dt = new Date(num);
  237. const now = new Date();
  238. const diff = now.getTime() - dt.getTime();
  239. const day = 1000 * 60 * 60 * 24;
  240. if (diff < day && !showFullDate) {
  241. return TimeFormatter.getTodayUnit();
  242. } else if (diff < 2 * day && !showFullDate) {
  243. return TimeFormatter.getYesterdayUnit();
  244. } else {
  245. var month = dt.getMonth() + 1
  246. var date = dt.getDate()
  247. if (global.language == 'en') {
  248. return TimeFormatter.getMonth(month) + ' ' + date
  249. }
  250. return TimeFormatter.getMonth(month) + date + '日'
  251. }
  252. }
  253. static getTimeByTimestamp(num: number): string {
  254. const dt = new Date(num);
  255. var hour = dt.getHours()
  256. var minute = dt.getMinutes()
  257. var formateTime = TimeFormatter.padZero(hour) + ':' + TimeFormatter.padZero(minute)
  258. return formateTime
  259. }
  260. //duration 根据起终点时间,忽略精度,如果两个时间点小于60s,则保留精度,否则为分钟
  261. static durationFormate(startTimestamp: number, endTimestamp: number) {
  262. var start = startTimestamp;
  263. var end = endTimestamp;
  264. const diff = Math.abs(end - start);
  265. if (diff > 60000) {
  266. var startTime = new Date(start)
  267. startTime.setSeconds(0)
  268. startTime.setMilliseconds(0)
  269. var endTime = new Date(end)
  270. endTime.setSeconds(0)
  271. endTime.setMilliseconds(0)
  272. start = startTime.getTime()
  273. end = endTime.getTime()
  274. }
  275. return TimeFormatter.calculateTimeDifference(start, end)
  276. }
  277. static durationFormate2(startTimestamp: number, endTimestamp: number) {
  278. var start = startTimestamp;
  279. var end = endTimestamp;
  280. const diff = Math.abs(end - start);
  281. if (diff > 60000) {
  282. var startTime = new Date(start)
  283. startTime.setSeconds(0)
  284. startTime.setMilliseconds(0)
  285. var endTime = new Date(end)
  286. endTime.setSeconds(0)
  287. endTime.setMilliseconds(0)
  288. start = startTime.getTime()
  289. end = endTime.getTime()
  290. }
  291. return TimeFormatter.calculateTimeDifference(start, end)
  292. }
  293. //计算时间间隔
  294. static calculateTimeDifference(startTimestamp: number, endTimestamp: number, ingoreSeconds?: boolean): string {
  295. const diff = Math.abs(endTimestamp - startTimestamp);
  296. // 计算小时、分钟和秒数
  297. const hours = Math.floor(diff / (1000 * 60 * 60));
  298. const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
  299. const seconds = Math.floor((diff % (1000 * 60)) / 1000);
  300. // 根据间隔的大小返回不同的格式
  301. if (diff < 60000) {
  302. return `${seconds}${TimeFormatter.getSecondsUnit(seconds)}`;
  303. } else if (diff < 3600000) {
  304. return `${minutes}${TimeFormatter.getMinutesUnit(minutes)}`;
  305. } else {
  306. if (minutes == 0) {
  307. return `${hours}${TimeFormatter.getHoursUnit(hours)}`;
  308. }
  309. return `${hours}${TimeFormatter.getHoursUnit(hours)}${minutes}${TimeFormatter.getMinutesUnit(minutes)}`;
  310. }
  311. }
  312. //格式化时间间隔
  313. static formateTimeDifference(startTimestamp: number, endTimestamp: number, ingoreSeconds?: boolean): string {
  314. const diff = Math.abs(endTimestamp - startTimestamp);
  315. // 计算小时、分钟和秒数
  316. const hours = Math.floor(diff / (1000 * 60 * 60));
  317. const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
  318. const seconds = Math.floor((diff % (1000 * 60)) / 1000);
  319. // 根据间隔的大小返回不同的格式
  320. if (diff < 60000) {
  321. return `00:00:${TimeFormatter.padZero(seconds)}`;
  322. } else if (diff < 3600000) {
  323. return `00:${TimeFormatter.padZero(minutes)}:${TimeFormatter.padZero(seconds)}`;
  324. } else {
  325. if (ingoreSeconds) return `${hours}${TimeFormatter.getHoursUnit(hours)}${minutes}${TimeFormatter.getMinutesUnit(minutes)}`;
  326. return `${TimeFormatter.padZero(hours)}:${TimeFormatter.padZero(minutes)}:${TimeFormatter.padZero(seconds)}`;
  327. }
  328. }
  329. static formateTime(timestamp: number) {
  330. const date = new Date(timestamp);
  331. return `${TimeFormatter.padZero(date.getMonth() + 1)}-${TimeFormatter.padZero(date.getDate())} ${TimeFormatter.padZero(date.getHours())}:${TimeFormatter.padZero(date.getMinutes())}:${TimeFormatter.padZero(date.getSeconds())}`;
  332. }
  333. static formateHourMinute(startTimestamp: number, endTimestamp: number): string {
  334. const diff = Math.abs(endTimestamp - startTimestamp);
  335. // 计算小时、分钟和秒数
  336. const hours = Math.floor(diff / (1000 * 60 * 60));
  337. const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
  338. return (hours < 10 ? `0${hours}` : `${hours}`) + ':' + (minutes < 10 ? `0${minutes}` : `${minutes}`);
  339. }
  340. static countdown = (dt: number, end = Date.now()): string => {
  341. // const end = Date.now();
  342. const time = end > dt ? Math.floor((end - dt) / 1000) : Math.ceil((dt - end) / 1000)//Math.ceil((end>dt?end-dt:dt-end)/1000);
  343. const hours = Math.floor(time / 3600);
  344. const minutes = Math.floor((time % 3600) / 60);
  345. const seconds = Math.floor(time % 60);
  346. return `${TimeFormatter.padZero(hours)}:${TimeFormatter.padZero(minutes)}:${TimeFormatter.padZero(seconds)}`;
  347. };
  348. //计算正计时
  349. static formateTimeNow = (dt: number, end = Date.now()): string => {
  350. // const end = Date.now();
  351. const time = Math.floor((end > dt ? end - dt : dt - end) / 1000);
  352. const hours = Math.floor(time / 3600);
  353. const minutes = Math.floor((time % 3600) / 60);
  354. const seconds = Math.floor(time % 60);
  355. return `${TimeFormatter.padZero(hours)}:${TimeFormatter.padZero(minutes)}:${TimeFormatter.padZero(seconds)}`;
  356. };
  357. static workoutTime = (time: number): string => {
  358. const hours = Math.floor(time / 3600);
  359. const minutes = Math.floor((time % 3600) / 60);
  360. const seconds = Math.floor(time % 60);
  361. return `${TimeFormatter.padZero(hours)}:${TimeFormatter.padZero(minutes)}:${TimeFormatter.padZero(seconds)}`;
  362. }
  363. static padZero = (num: number): string => {
  364. return num.toString().padStart(2, '0');
  365. };
  366. //根据时间段和一个时间,推算另外一个时间
  367. static calculateTimeByTimeRange = (timeRange: number, strTime: string, isStart: boolean): string => {
  368. console.log(timeRange, strTime, isStart)
  369. var list = strTime.split(':');
  370. var time = parseInt(list[0]) * 60 + parseInt(list[1]);
  371. if (isStart) {
  372. time = time + timeRange;
  373. }
  374. else {
  375. time = time - timeRange;
  376. }
  377. time = time < 0 ? time + 24 * 60 : time;
  378. time = time >= 24 * 60 ? time - 24 * 60 : time;
  379. var hour = Math.floor(time / 60);
  380. var minute = time % 60;
  381. return `${TimeFormatter.padZero(hour)}:${TimeFormatter.padZero(minute)}`;
  382. }
  383. static formateDurationBySeconds = (left: number) => {
  384. const hours = Math.floor(left / 3600);
  385. const minutes = Math.floor((left % 3600) / 60);
  386. const seconds = Math.floor(left % 60);
  387. var str = ''
  388. if (hours > 0) {
  389. str = str + hours + TimeFormatter.getHoursUnit(hours)
  390. }
  391. if (minutes > 0) {
  392. str = str + minutes + TimeFormatter.getMinutesUnit(minutes)
  393. }
  394. if (seconds > 0) {
  395. str = str + seconds + TimeFormatter.getSecondsUnit(seconds)
  396. }
  397. return str
  398. }
  399. static workoutTimeAndUnitList = (count: number) => {
  400. const hours = Math.floor(count / 3600);
  401. const minutes = Math.floor((count % 3600) / 60);
  402. const seconds = Math.floor(count % 60);
  403. var values: any = []
  404. var units: any = []
  405. if (hours > 0) {
  406. values.push(hours)
  407. units.push(TimeFormatter.getHoursUnit(hours))
  408. }
  409. if (minutes > 0) {
  410. values.push(minutes)
  411. units.push(TimeFormatter.getMinutesUnit(minutes))
  412. }
  413. if (seconds > 0) {
  414. values.push(seconds)
  415. units.push(TimeFormatter.getSecondsUnit(seconds))
  416. }
  417. return {
  418. values: values,
  419. units: units
  420. }
  421. }
  422. //获取今天的日期和星期几
  423. static getDateAndWeek = (timestamp: number, ignore = false) => {
  424. const now = new Date();
  425. var dt = new Date(timestamp)
  426. dt.setSeconds(0)
  427. dt.setMilliseconds(0)
  428. dt.setMinutes(0)
  429. dt.setHours(0)
  430. const diff = now.getTime() - dt.getTime();
  431. const day = 1000 * 60 * 60 * 24;
  432. if (diff < day && !ignore) {
  433. return TimeFormatter.getTodayUnit();
  434. } else if (diff < 2 * day && !ignore) {
  435. return TimeFormatter.getYesterdayUnit();
  436. } else {
  437. if (global.language == 'en') {
  438. return `${TimeFormatter.getDayOfWeek(dt.getDay())} ${TimeFormatter.getMonth(dt.getMonth() + 1)} ${dt.getDate()}`
  439. }
  440. return `${TimeFormatter.getMonth(dt.getMonth() + 1)}${dt.getDate()}日 ${TimeFormatter.getDayOfWeek(dt.getDay())}`
  441. }
  442. }
  443. //获取当前时间(hh:mm)
  444. static getCurrentHourAndMinute = () => {
  445. var now = new Date()
  446. // var localOffset = now.getTimezoneOffset()
  447. // console.log(localOffset)
  448. // debugger
  449. return `${TimeFormatter.padZero(now.getHours())}:${TimeFormatter.padZero(now.getMinutes())}`;
  450. }
  451. //获取时长单位
  452. static getSecondsUnit = (seconds, isFull?: boolean) => {
  453. if (global.language == 'en') {
  454. if (isFull) {
  455. return seconds > 1 ? ' seconds ' : ' second '
  456. }
  457. return seconds > 1 ? ' secs ' : ' sec '
  458. }
  459. return '秒'
  460. }
  461. static getMinutesUnit = (minutes, isFull?: boolean) => {
  462. if (global.language == 'en') {
  463. if (isFull) {
  464. return minutes > 1 ? ' minutes ' : ' minute '
  465. }
  466. return minutes > 1 ? ' mins ' : ' min '
  467. }
  468. return '分钟'
  469. }
  470. static getHoursUnit = (hours, isFull?: boolean) => {
  471. if (global.language == 'en') {
  472. if (isFull) {
  473. return hours > 1 ? ' hours ' : ' hour '
  474. }
  475. return hours > 1 ? ' hrs ' : ' hr '
  476. }
  477. return '小时'
  478. }
  479. static getTodayUnit = () => {
  480. return global.language == 'en' ? 'Today' : '今天'
  481. }
  482. static getYesterdayUnit = () => {
  483. return global.language == 'en' ? 'Yesterday' : '昨天'
  484. }
  485. static getTomorrowUnit = () => {
  486. return global.language == 'en' ? 'Tomorrow' : '明天'
  487. }
  488. static getJustNowUnit = () => {
  489. return global.language == 'en' ? 'Just now' : '刚刚'
  490. }
  491. static getDayOfWeek = (index, isFull?: boolean) => {
  492. var weeks = ['日', '一', '二', '三', '四', '五', '六']
  493. var weeks2 = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
  494. ];
  495. var weeksFull = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
  496. if (global.language == 'en') {
  497. if (isFull) {
  498. return weeksFull[index]
  499. }
  500. return weeks2[index]
  501. }
  502. return '星期' + weeks[index]
  503. }
  504. static getMonth = (index) => {
  505. const monthNames = [
  506. 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  507. 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
  508. ];
  509. if (global.language == 'en') {
  510. return monthNames[index - 1]
  511. }
  512. return index + '月'
  513. }
  514. //获取本周一零点的时间戳
  515. static getMondayTimestamp = () => {
  516. const now = new Date();
  517. const currentDay = now.getDay(); // Get the current day of the week (0 for Sunday, 1 for Monday, etc.)
  518. const startOfWeek = new Date(now); // Create a new Date object with the current date and time
  519. // Calculate the difference in milliseconds to the beginning of the week
  520. const diffToMonday = (currentDay - 1) * 24 * 60 * 60 * 1000; // Subtract 1 day worth of milliseconds for each day after Monday
  521. startOfWeek.setTime(now.getTime() - diffToMonday); // Set the time to the beginning of the week (Monday at 00:00:00)
  522. startOfWeek.setHours(0)
  523. startOfWeek.setMinutes(0)
  524. startOfWeek.setSeconds(0)
  525. startOfWeek.setMilliseconds(0)
  526. const timestamp = startOfWeek.getTime(); // Get the timestamp in milliseconds
  527. return timestamp
  528. }
  529. //hh:mm转换成秒数
  530. static timestringToSeconds = (strTime: string) => {
  531. var list = strTime.split(':')
  532. return parseInt(list[0]) * 3600 + parseInt(list[1]) * 60
  533. }
  534. }