dates.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // @ts-nocheck
  2. import {dayuts, Dayuts} from "./index"
  3. // import {DayutsConfig} from '../../types/date.uts'
  4. import { DayutsConfig } from '../utssdk/interface'
  5. export class Dates {
  6. /**
  7. * Dayuts实例
  8. */
  9. newDate() : Dayuts;
  10. newDate(date : string) : Dayuts;
  11. newDate(date : any[]) : Dayuts;
  12. newDate(date : number) : Dayuts;
  13. newDate(date : UTSJSONObject) : Dayuts;
  14. newDate(date : Date) : Dayuts;
  15. newDate(date : Dayuts) : Dayuts;
  16. // #ifndef APP-ANDROID || APP-IOS || APP-HARMONY
  17. newDate(date : any | null, format : string) : Dayuts;
  18. newDate(date : any | null, format : string | null, locale : string | null) : Dayuts;
  19. // #endif
  20. newDate(date : any | null = null, format : string | null = null, locale : string | null = null) : Dayuts {
  21. if (date != null && date instanceof Dayuts) return date.clone()
  22. return new Dayuts({
  23. date,
  24. format,
  25. locale
  26. } as DayutsConfig)
  27. }
  28. /**
  29. * 万能转日期对象
  30. * @param date 时间
  31. */
  32. toDate(date : string) : Date {
  33. date = date.split('周')[0].trim().split('星期')[0].trim().split('礼拜')[0].trim()
  34. if (date == '') {
  35. return new Date()
  36. }
  37. if (date.length >= 10 && /^\d+$/.test(date)) {
  38. // 时间戳
  39. let timestamp = parseInt(date)
  40. // 若为unix秒时间戳,则转为毫秒时间戳
  41. if (/^\d{10}$/.test(timestamp.toString())) {
  42. return new Date(timestamp * 1000)
  43. } else {
  44. return new Date(timestamp)
  45. }
  46. } else {
  47. if (!date.includes('T')) {
  48. // 容错
  49. date = date.replace(/\//g, '-').
  50. replace(/年/g, '-').
  51. replace(/月/g, '-').
  52. replace(/日/g, '').
  53. replace(/时/g, ':').
  54. replace(/分/g, ':').
  55. replace(/秒/g, '').
  56. replace(/^-+|-+$/g, '').
  57. trim()
  58. // 补全
  59. if (date.length == 4) {
  60. date += '-01-01 00:00:00'
  61. } else if (date.length == 7) {
  62. date += '-01 00:00:00'
  63. } else if (date.length == 8) {
  64. date = `${this.today()} ${date}`
  65. } else if (date.length == 10) {
  66. date += ' 00:00:00'
  67. } else if (date.length == 13) {
  68. date += ':00:00'
  69. } else if (date.length == 16) {
  70. date += ':00'
  71. }
  72. let d = date.split(/[^0-9]/)
  73. try{
  74. return new Date(parseInt(d[0]), parseInt(d[1]) - 1, parseInt(d[2]), parseInt(d[3]), parseInt(d[4]), parseInt(d[5]))
  75. }catch(e){
  76. console.error(`[ux-date]解析失败:${date}`)
  77. return new Date()
  78. }
  79. } else {
  80. return new Date(date)
  81. }
  82. }
  83. }
  84. /**
  85. * 万能格式化日期
  86. * @param date 时间
  87. * @param format 格式化规则 支持yyyy-MM-dd|yyyy-MM-dd HH:mm:ss|yyyy/MM/dd|yyyy/MM/dd HH:mm:ss|yyyy年MM月dd日等组合 默认yyyy-mm-dd
  88. */
  89. fmtDate(date : string, format : string) : string {
  90. if(format == '') {
  91. format = 'yyyy-MM-dd'
  92. }
  93. date = date.split('周')[0].trim().split('星期')[0].trim().split('礼拜')[0].trim()
  94. let d = this.toDate(date)
  95. let timeSource = new Map<string, string>()
  96. timeSource.set('y', d.getFullYear().toString())
  97. timeSource.set('M', (d.getMonth() + 1).toString().padStart(2, '0'))
  98. timeSource.set('d', d.getDate().toString().padStart(2, '0'))
  99. timeSource.set('H', d.getHours().toString().padStart(2, '0'))
  100. timeSource.set('m', d.getMinutes().toString().padStart(2, '0'))
  101. timeSource.set('s', d.getSeconds().toString().padStart(2, '0'))
  102. let result = format.split('周')[0].trim().split('星期')[0].trim().split('礼拜')[0].trim()
  103. timeSource.forEach((v : string, key : string) => {
  104. const rets = new RegExp(`${key}+`).exec(result) ?? [] as RegExp[]
  105. if (rets.length > 0) {
  106. result = result.replace(rets[0].toString(), v)
  107. }
  108. })
  109. let fmtWeek = format.indexOf('周') != -1 || format.indexOf('星期') != -1 || format.indexOf('礼拜') != -1
  110. if(fmtWeek) {
  111. result += ` ${this.weekName(this.toDate(result).getDay(), format)}`
  112. }
  113. return result
  114. }
  115. /**
  116. * 现在 yyyy-MM-dd HH:mm:ss
  117. */
  118. now() : string {
  119. let date = new Date()
  120. let year = date.getFullYear()
  121. let month = `${date.getMonth() + 1}`.padStart(2, '0')
  122. let day = `${date.getDate()}`.padStart(2, '0')
  123. let hour = `${date.getHours()}`.padStart(2, '0')
  124. let minute = `${date.getMinutes()}`.padStart(2, '0')
  125. let second = `${date.getSeconds()}`.padStart(2, '0')
  126. return `${year}-${month}-${day} ${hour}:${minute}:${second}`
  127. }
  128. /**
  129. * 今天
  130. * @param n n为负则代表取前n天,为正则代表取后n天,0则为今天
  131. */
  132. today(n : number = 0) : string {
  133. let date = new Date()
  134. date.setDate(date.getDate() + n)
  135. let year = date.getFullYear()
  136. let month = `${date.getMonth() + 1}`.padStart(2, '0')
  137. let day = `${date.getDate()}`.padStart(2, '0')
  138. return `${year}-${month}-${day}`
  139. }
  140. /**
  141. * 本周所有日期
  142. */
  143. weeks() : string[] {
  144. const fDate = new Date()
  145. const eDate = new Date()
  146. fDate.setDate(fDate.getDate() - fDate.getDay() + 1)
  147. eDate.setDate(eDate.getDate() - eDate.getDay() + 7)
  148. const result : string[] = []
  149. for (let d = fDate; d.getTime() <= eDate.getTime(); d.setDate(d.getDate() + 1)) {
  150. result.push(d.toISOString().slice(0, 10))
  151. }
  152. return result;
  153. }
  154. /**
  155. * 周几
  156. */
  157. weekName(day: number, format: string) : string {
  158. if(format.indexOf('星期') != -1) {
  159. return ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'][day]
  160. } else if(format.indexOf('礼拜') != -1 ) {
  161. return ['礼拜天', '礼拜一', '礼拜二', '礼拜三', '礼拜四', '礼拜五', '礼拜六'][day]
  162. } else {
  163. return ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][day]
  164. }
  165. }
  166. }