index.uts 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. // @ts-nocheck
  2. import { DayutsConfig, type DayutsLocale, DayutsUnit, DayutsObject } from '../utssdk/interface'
  3. import { REGEX_FORMAT, REGEX_PARSE, INVALID_DATE_STRING, M, Y, W, D, DATE, H, MIN, S, MS, Q, MILLISECONDS_A_MINUTE, MILLISECONDS_A_HOUR, MILLISECONDS_A_SECOND, MILLISECONDS_A_WEEK, MILLISECONDS_A_DAY, FORMAT_DEFAULT } from './constant'
  4. import { isNumber, prettyUnit, padStart, padZoneStr, monthDiff, absFloor } from './utils'
  5. import { dayutsIntl, localeState } from './use'
  6. type Threshold = {
  7. l : string;
  8. r ?: number;
  9. d ?: DayutsUnit;
  10. }
  11. function parseLocale(preset : string | null) : string | null;
  12. function parseLocale(preset : DayutsLocale | null) : string | null;
  13. function parseLocale(preset : string, object : DayutsLocale | null, isLocal : boolean) : string | null;
  14. function parseLocale(preset : DayutsLocale, object : DayutsLocale, isLocal : boolean) : string | null;
  15. function parseLocale(preset : any | null, object : DayutsLocale | null = null, isLocal : boolean = false) : string | null {
  16. let l : string | null = null
  17. if (preset == null) return dayutsIntl.locale
  18. if (typeof preset == 'string') {
  19. const presetLower = (preset as string).toLowerCase()
  20. if (dayutsIntl.has(presetLower)) {
  21. l = presetLower
  22. }
  23. if (object != null) {
  24. dayutsIntl.set(presetLower, object)
  25. l = presetLower
  26. }
  27. const presetSplit = preset.split('-')
  28. if (l == null && presetSplit.length > 1) {
  29. return parseLocale(presetSplit[0])
  30. }
  31. } else if (preset instanceof DayutsLocale) {
  32. // const { name } = preset as DayutsLocale
  33. dayutsIntl.set(preset.name, preset)
  34. l = preset.name
  35. }
  36. if (!isLocal && l != null) {
  37. // L = l
  38. dayutsIntl.locale = l
  39. }
  40. // return l ?? L //(!isLocal && L != null)
  41. return l ?? dayutsIntl.locale //(!isLocal && L != null)
  42. }
  43. function tryParseNumberAtIndex(digits : (any|null)[], index : number) : number | null {
  44. // 检查索引是否在数组范围内
  45. if (index >= 0 && index < digits.length) {
  46. if(digits[index] == null) return null
  47. // 尝试解析索引位置的字符串为数字
  48. const parsedNumber = isNumber(digits[index]) ? digits[index] as number : parseInt(`${digits[index]}`, 10);
  49. // 检查解析结果是否为有效数字
  50. if (!isNaN(parsedNumber)) {
  51. return parsedNumber;
  52. }
  53. }
  54. return null
  55. }
  56. // function createDateFromArray(d: number[]):Date
  57. // function createDateFromArray(d: string[]):Date
  58. function createDateFromArray(d : (any|null)[], offset : number = 0) : Date {
  59. const year = tryParseNumberAtIndex(d, 1 - offset) ?? new Date().getFullYear()
  60. const month = (tryParseNumberAtIndex(d, 2 - offset) ?? 1) - 1
  61. const day = tryParseNumberAtIndex(d, 3 - offset) ?? 1
  62. const hour = tryParseNumberAtIndex(d, 4 - offset) ?? 0
  63. const minute = tryParseNumberAtIndex(d, 5 - offset) ?? 0
  64. const second = tryParseNumberAtIndex(d, 6 - offset) ?? 0
  65. const millisecond = (tryParseNumberAtIndex(d, 7 - offset) ?? 0).toString().substring(0, 3) //d.length > 7 ? parseInt((d[7] ?? '0').substring(0, 3)) : 0
  66. return new Date(
  67. year,
  68. month,
  69. day,
  70. hour,
  71. minute,
  72. second,
  73. parseInt(millisecond))
  74. }
  75. function parseDate(cfg : DayutsConfig) : Date|null {
  76. const { date } = cfg
  77. if (date == null) return new Date()
  78. if (date instanceof Date) return date as Date
  79. try {
  80. if (typeof date == 'string' && !/Z$/i.test(date as string)) {
  81. const d = date.match(REGEX_PARSE)
  82. // #ifndef APP-ANDROID || APP-IOS
  83. const isNull = d == null
  84. // #endif
  85. // #ifdef APP-ANDROID || APP-IOS
  86. const isNull = d == null|| Array.isArray(d) && d.length == 0
  87. // #endif
  88. if (!isNull) {
  89. return createDateFromArray(d as (any|null)[])
  90. }
  91. }
  92. if (typeof date == 'string') return new Date(date as string)
  93. if (Array.isArray(date)) {
  94. return createDateFromArray(date as (any|null)[], 1)
  95. }
  96. if (isNumber(date)) return new Date(date as number)
  97. return null//new Date()
  98. } catch(err) {
  99. return null//new Date()
  100. }
  101. }
  102. function wrapper(date : any, instance : Dayuts) : Dayuts {
  103. return dayuts(date, instance.$L)
  104. }
  105. export class Dayuts {
  106. $L : string
  107. private valid: boolean = true;
  108. private $d : Date = new Date()
  109. private $y : number = 0
  110. private $M : number = 0
  111. private $D : number = 0
  112. private $W : number = 0
  113. private $H : number = 0
  114. private $m : number = 0
  115. private $s : number = 0
  116. private $ms : number = 0
  117. private $u : boolean = false
  118. constructor(cfg : DayutsConfig) {
  119. this.$L = parseLocale(cfg.locale) ?? dayutsIntl.locale //'en'
  120. this.parse(cfg)
  121. }
  122. parse(cfg : DayutsConfig) {
  123. const _d = parseDate(cfg)
  124. if(_d != null) {
  125. this.$d = parseDate(cfg)!
  126. this.init()
  127. } else {
  128. this.valid = false
  129. }
  130. }
  131. init() {
  132. const { $d } = this
  133. this.$y = $d.getFullYear()
  134. this.$M = $d.getMonth()
  135. this.$D = $d.getDate()
  136. this.$W = $d.getDay()
  137. this.$H = $d.getHours()
  138. this.$m = $d.getMinutes()
  139. this.$s = $d.getSeconds()
  140. this.$ms = $d.getMilliseconds()
  141. }
  142. /**
  143. * 检查日期对象是否有效。
  144. *
  145. * @returns {boolean} 如果日期对象有效,则返回true;否则返回false。
  146. */
  147. isValid() : boolean {
  148. return this.valid
  149. // return !(this.$d.toString() == INVALID_DATE_STRING)
  150. }
  151. /**
  152. * 检查当前日期是否与给定的日期在指定的时间单位内相同。
  153. *
  154. * @param {string|number|Date} input - 要比较的日期。
  155. * @param {string} units - 时间单位,例如'year'、'month'、'day'等。
  156. * @returns {boolean} 如果当前日期与给定的日期在指定的时间单位内相同,则返回true;否则返回false。
  157. */
  158. isSame(input : string) : boolean
  159. isSame(input : number) : boolean
  160. isSame(input : Date) : boolean
  161. isSame(input : Dayuts) : boolean
  162. isSame(input : UTSJSONObject) : boolean
  163. isSame(input : string, units : DayutsUnit) : boolean
  164. isSame(input : number, units : DayutsUnit) : boolean
  165. isSame(input : Date, units : DayutsUnit) : boolean
  166. isSame(input : Dayuts, units : DayutsUnit) : boolean
  167. isSame(input : UTSJSONObject, units : DayutsUnit) : boolean
  168. isSame(input : any, units : DayutsUnit = 'millisecond') : boolean {
  169. const other = input instanceof Dayuts ? input : dayuts(input)
  170. const date1 = this.startOf(units).valueOf()
  171. const date2 = other.valueOf()
  172. const date3 = this.endOf(units).valueOf()
  173. return date1 <= date2 && date2 <= date3
  174. }
  175. /**
  176. * 检查给定的日期或时间是否在当前 dayuts 对象的指定时间单位之后。
  177. *
  178. * @param {string | number | Date | Dayuts} input - 要与当前 dayuts 对象进行比较的日期或时间。
  179. * @param {string} units - 要比较的时间单位(如 "year"、"month"、"day" 等)。
  180. * @returns {boolean} 如果给定的日期或时间在当前 dayuts 对象的指定时间单位之后,则返回 `true`,否则返回 `false`。
  181. */
  182. isAfter(input : string) : boolean
  183. isAfter(input : number) : boolean
  184. isAfter(input : Date) : boolean
  185. isAfter(input : Dayuts) : boolean
  186. isAfter(input : UTSJSONObject) : boolean
  187. isAfter(input : string, units : DayutsUnit) : boolean
  188. isAfter(input : number, units : DayutsUnit) : boolean
  189. isAfter(input : Date, units : DayutsUnit) : boolean
  190. isAfter(input : Dayuts, units : DayutsUnit) : boolean
  191. isAfter(input : UTSJSONObject, units : DayutsUnit) : boolean
  192. isAfter(input : any, units : DayutsUnit = 'millisecond') : boolean {
  193. const other = input instanceof Dayuts ? input : dayuts(input)
  194. const date1 = other.valueOf()
  195. const date2 = this.startOf(units).valueOf()
  196. return date1 < date2;
  197. }
  198. /**
  199. * 检查给定的日期或时间是否在当前 dayuts 对象的指定时间单位之前。
  200. *
  201. * @param {string | number | Date | Dayuts} input - 要与当前 dayuts 对象进行比较的日期或时间。
  202. * @param {string} units - 要比较的时间单位(如 "year"、"month"、"day" 等)。
  203. * @returns {boolean} 如果给定的日期或时间在当前 dayuts 对象的指定时间单位之前,则返回 `true`,否则返回 `false`。
  204. */
  205. isBefore(input : string) : boolean
  206. isBefore(input : number) : boolean
  207. isBefore(input : Date) : boolean
  208. isBefore(input : Dayuts) : boolean
  209. isBefore(input : UTSJSONObject) : boolean
  210. isBefore(input : string, units : DayutsUnit) : boolean
  211. isBefore(input : number, units : DayutsUnit) : boolean
  212. isBefore(input : Date, units : DayutsUnit) : boolean
  213. isBefore(input : Dayuts, units : DayutsUnit) : boolean
  214. isBefore(input : UTSJSONObject, units : DayutsUnit) : boolean
  215. isBefore(input : any, units : DayutsUnit = 'millisecond') : boolean {
  216. const other = input instanceof Dayuts ? input : dayuts(input);
  217. const date1 = other.valueOf()
  218. const date2 = this.endOf(units).valueOf()
  219. return date2 < date1;
  220. }
  221. /**
  222. * 判断当前Dayuts对象是否与给定的输入在同一时间或之前,根据指定的时间单位
  223. * @param {(string | number | Date | Dayuts | UTSJSONObject)} input - 输入的时间
  224. * @param {DayutsUnit} units - 指定的时间单位
  225. * @returns {boolean} - 如果当前Dayuts对象与给定的输入在同一时间或之前,则返回true,否则返回false
  226. */
  227. isSameOrBefore(input : string) : boolean
  228. isSameOrBefore(input : number) : boolean
  229. isSameOrBefore(input : Date) : boolean
  230. isSameOrBefore(input : Dayuts) : boolean
  231. isSameOrBefore(input : UTSJSONObject) : boolean
  232. isSameOrBefore(input : string, units : DayutsUnit) : boolean
  233. isSameOrBefore(input : number, units : DayutsUnit) : boolean
  234. isSameOrBefore(input : Date, units : DayutsUnit) : boolean
  235. isSameOrBefore(input : Dayuts, units : DayutsUnit) : boolean
  236. isSameOrBefore(input : UTSJSONObject, units : DayutsUnit) : boolean
  237. isSameOrBefore(input : any, units : DayutsUnit = 'millisecond') : boolean {
  238. return this.isSame(input, units) || this.isBefore(input, units)
  239. }
  240. /**
  241. * 判断当前Dayuts对象是否与给定的输入在同一时间或之后,根据指定的时间单位
  242. * @param {(string | number | Date | Dayuts | UTSJSONObject)} input - 输入的时间
  243. * @param {DayutsUnit} units - 指定的时间单位
  244. * @returns {boolean} - 如果当前Dayuts对象与给定的输入在同一时间或之后,则返回true,否则返回false
  245. */
  246. isSameOrAfter(input : string) : boolean
  247. isSameOrAfter(input : number) : boolean
  248. isSameOrAfter(input : Date) : boolean
  249. isSameOrAfter(input : Dayuts) : boolean
  250. isSameOrAfter(input : UTSJSONObject) : boolean
  251. isSameOrAfter(input : string, units : DayutsUnit) : boolean
  252. isSameOrAfter(input : number, units : DayutsUnit) : boolean
  253. isSameOrAfter(input : Date, units : DayutsUnit) : boolean
  254. isSameOrAfter(input : Dayuts, units : DayutsUnit) : boolean
  255. isSameOrAfter(input : UTSJSONObject, units : DayutsUnit) : boolean
  256. isSameOrAfter(input : any, units : DayutsUnit = 'millisecond') : boolean {
  257. return this.isSame(input, units) || this.isAfter(input, units)
  258. }
  259. /**
  260. * 判断当前Dayuts对象是否在给定的两个时间之间
  261. * @param {any} input - 第一个时间输入
  262. * @param {any} input2 - 第二个时间输入
  263. * @param {DayutsUnit} units - 指定的时间单位
  264. * @param {string} interval - 区间符号,表示区间的开闭性,默认为'()',表示开区间
  265. * @returns {boolean} - 如果当前Dayuts对象在给定的两个时间之间,则返回true,否则返回false
  266. */
  267. isBetween(input : any, input2 : any, units : DayutsUnit = 'millisecond', interval : string = '()') : boolean {
  268. const dA = dayuts(input)
  269. const dB = dayuts(input2)
  270. const dAi = interval.startsWith('(')
  271. const dBi = interval.endsWith(')')
  272. return ((dAi ? this.isAfter(dA, units) : !this.isBefore(dA, units)) &&
  273. (dBi ? this.isBefore(dB, units) : !this.isAfter(dB, units)))
  274. || ((dAi ? this.isBefore(dA, units) : !this.isAfter(dA, units)) &&
  275. (dBi ? this.isAfter(dB, units) : !this.isBefore(dB, units)))
  276. }
  277. /**
  278. * 判断当前Dayuts对象所在的年份是否为闰年
  279. * @returns {boolean} - 如果当前Dayuts对象所在的年份是闰年,则返回true,否则返回false
  280. */
  281. isLeapYear():boolean{
  282. return ((this.$y % 4 == 0) && (this.$y % 100 != 0)) || (this.$y % 400 == 0)
  283. }
  284. isToday():boolean{
  285. const comparisonTemplate = 'YYYY-MM-DD'
  286. const now = dayuts()
  287. return this.format(comparisonTemplate) == now.format(comparisonTemplate)
  288. }
  289. /**
  290. * 获取当前 `dayuts` 对象的 Unix 时间戳(以秒为单位)。
  291. *
  292. * @returns {number} 返回当前 `dayuts` 对象的 Unix 时间戳(以秒为单位)。
  293. */
  294. unix() : number {
  295. return Math.floor(this.valueOf() / 1000);
  296. }
  297. /**
  298. * 将当前日期设置为指定时间单位的开始或结束。
  299. *
  300. * @param {string} units - 时间单位,例如'year'、'month'、'day'等。
  301. * @param {boolean} startOf - 如果为true,则设置为开始;如果为false,则设置为结束。
  302. * @returns {Dayuts} 返回一个新的Dayuts对象,表示调整后的日期。
  303. */
  304. startOf(units : DayutsUnit, startOf : boolean = true) : Dayuts {
  305. const isStartOf = startOf;
  306. const unit = prettyUnit(units)
  307. // instanceFactory 函数用于创建一个新的 Dayuts 对象,表示给定日期的开始或结束。
  308. // 参数 d 和 m 分别表示日期和月份。
  309. const instanceFactory = (d : number, m : number) : Dayuts => {
  310. const ins = dayuts(new Date(this.$y, m, d))
  311. return isStartOf ? ins : ins.endOf(D)
  312. }
  313. // instanceFactorySet 函数用于创建一个新的 Dayuts 对象,表示调整后的时间。
  314. // 参数 method 表示要调用的 Date 对象的方法(如 'setHours'),slice 表示要调整的时间部分的索引。
  315. const instanceFactorySet = (method : string, slice : number) : Dayuts => {
  316. // 定义表示开始和结束时间的参数数组。
  317. const argumentStart = [0, 0, 0, 0]
  318. const argumentEnd = [23, 59, 59, 999]
  319. // 根据 isStartOf 的值,选择开始或结束时间的参数数组,并调用 Date 对象的方法。
  320. const args = (isStartOf ? argumentStart : argumentEnd).slice(slice)
  321. const date = this.toDate()
  322. if (method == 'setHours') {
  323. date.setHours(args[0]);
  324. date.setMinutes(args[1])
  325. date.setSeconds(args[2])
  326. date.setMilliseconds(args[3])
  327. } else if (method == 'setMinutes') {
  328. date.setMinutes(args[0]);
  329. date.setSeconds(args[1])
  330. date.setMilliseconds(args[2])
  331. } else if (method == 'setSeconds') {
  332. date.setSeconds(args[0])
  333. date.setMilliseconds(args[1])
  334. } else if (method == 'setMilliseconds') {
  335. date.setMilliseconds(args[0])
  336. }
  337. return dayuts(date)
  338. }
  339. const { $W, $M, $D } = this
  340. const utcPad = `set${this.$u ? 'UTC' : ''}`
  341. if (unit == Y) {
  342. return isStartOf ? instanceFactory(1, 0) : instanceFactory(31, 11);
  343. } else if (unit == M) {
  344. return isStartOf ? instanceFactory(1, $M) : instanceFactory(0, $M + 1);
  345. } else if (unit == W) {
  346. const weekStart = this.$locale().weekStart ?? 0;
  347. const gap = ($W < weekStart ? $W + 7 : $W) - weekStart;
  348. return instanceFactory(isStartOf ? $D - gap : $D + (6 - gap), $M);
  349. } else if (unit == D || unit == DATE) {
  350. return instanceFactorySet(`${utcPad}Hours`, 0);
  351. } else if (unit == H) {
  352. return instanceFactorySet(`${utcPad}Minutes`, 1);
  353. } else if (unit == MIN) {
  354. return instanceFactorySet(`${utcPad}Seconds`, 2);
  355. } else if (unit == S) {
  356. return instanceFactorySet(`${utcPad}Milliseconds`, 3);
  357. } else {
  358. return this.clone();
  359. }
  360. }
  361. /**
  362. * 将当前日期设置为指定时间单位的结束。
  363. *
  364. * @param {string} arg - 时间单位,例如'year'、'month'、'day'等。
  365. * @returns {Dayuts} 返回一个新的Dayuts对象,表示调整后的日期。
  366. */
  367. endOf(units : DayutsUnit) : Dayuts {
  368. return this.startOf(units, false)
  369. }
  370. /**
  371. * 设置指定的时间单位的值。
  372. *
  373. * @param {string} units - 要设置的时间单位(如 "year"、"month"、"day" 等)。
  374. * @param {number} int - 要设置的值。
  375. * @returns {Dayuts} 返回当前对象。
  376. */
  377. private $set(units : DayutsUnit, int : number) : Dayuts { // private set
  378. const unit = prettyUnit(units)
  379. // const utcPad = `set${this.$u ? 'UTC' : ''}`
  380. const arg = unit == D ? this.$D + (int - this.$W) : int
  381. const setDateUnit = (date : Dayuts, unit : DayutsUnit, arg : number) => {
  382. if (unit == D || unit == DATE) {
  383. date.$d.setDate(arg);
  384. } else if (unit == M) {
  385. date.$d.setMonth(arg);
  386. } else if (unit == Y) {
  387. date.$d.setFullYear(arg);
  388. } else if (unit == H) {
  389. date.$d.setHours(arg);
  390. } else if (unit == MIN) {
  391. date.$d.setMinutes(arg);
  392. } else if (unit == S) {
  393. date.$d.setSeconds(arg);
  394. } else if (unit == MS) {
  395. date.$d.setMilliseconds(arg);
  396. }
  397. }
  398. if (unit == M || unit == Y) {
  399. // clone is for badMutable plugin
  400. const date = this.clone().set(DATE, 1)
  401. // date.$d[name](arg)
  402. setDateUnit(date, unit, arg)
  403. date.init()
  404. this.$d = date.set(DATE, Math.min(this.$D, date.daysInMonth())).$d
  405. } else {
  406. setDateUnit(this, unit, arg)
  407. // this.$d[name](arg)
  408. }
  409. this.init()
  410. return this
  411. }
  412. /**
  413. * 创建一个当前对象的副本,并设置指定的时间单位的值。
  414. *
  415. * @param {string} string - 要设置的时间单位(如 "year"、"month"、"day" 等)。
  416. * @param {number} int - 要设置的值。
  417. * @returns {Dayuts} 返回一个新的 `dayuts` 对象,其值为当前对象的副本,并设置了指定的时间单位的值。
  418. */
  419. set(string : DayutsUnit, int : number) : Dayuts {
  420. return this.clone().$set(string, int);
  421. }
  422. /**
  423. * 获取当前 `dayuts` 对象的指定时间单位的值。
  424. *
  425. * @param {string} units - 要获取的时间单位(如 "year"、"month"、"day" 等)。
  426. * @returns {number} 返回当前 `dayuts` 对象的指定时间单位的值。
  427. */
  428. get(units : DayutsUnit) : number {
  429. const unit = prettyUnit(units)
  430. if (unit == D) {
  431. return this.day()
  432. } else if (unit == DATE) {
  433. return this.date()
  434. } else if (unit == M) {
  435. return this.month()
  436. } else if (unit == Y) {
  437. return this.year()
  438. } else if (unit == H) {
  439. return this.hour()
  440. } else if (unit == MIN) {
  441. return this.minute()
  442. } else if (unit == S) {
  443. return this.second()
  444. } else if (unit == MS) {
  445. return this.millisecond()
  446. }
  447. return 0
  448. }
  449. /**
  450. * 获取或设置年份。
  451. * @param {number | null} [input] - 要设置的年份。
  452. * @returns {number | Dayuts} 年份或 Dayuts 实例。
  453. */
  454. year() : number
  455. year(input : number) : Dayuts
  456. year(input : number | null = null) : any {
  457. if (input == null) return this.$y
  458. return this.set(Y, input)
  459. }
  460. /**
  461. * 获取或设置月份。
  462. * @param {number | null} [input] - 要设置的月份(0-11)。
  463. * @returns {number | Dayuts} 月份或 Dayuts 实例。
  464. */
  465. month() : number
  466. month(input : number) : Dayuts
  467. month(input : number | null = null) : any {
  468. if (input == null) return this.$M
  469. return this.set(M, input)
  470. }
  471. /**
  472. * 获取或设置星期几。
  473. * @param {number | null} [input] - 要设置的星期几(0-6)。
  474. * @returns {number | Dayuts} 星期几或 Dayuts 实例。
  475. */
  476. day() : number
  477. day(input : number) : Dayuts
  478. day(input : number | null = null) : any {
  479. if (input == null) return this.$W
  480. return this.set(D, input)
  481. }
  482. /**
  483. * 获取或设置月份中的某一天。
  484. * @param {number | null} [input] - 要设置的月份中的某一天(1-31)。
  485. * @returns {number | Dayuts} 月份中的某一天或 Dayuts 实例。
  486. */
  487. date() : number
  488. date(input : number) : Dayuts
  489. date(input : number | null = null) : any {
  490. if (input == null) return this.$D
  491. return this.set(DATE, input)
  492. }
  493. /**
  494. * 获取或设置小时。
  495. * @param {number | null} [input] - 要设置的小时(0-23)。
  496. * @returns {number | Dayuts} 小时或 Dayuts 实例。
  497. */
  498. hour() : number
  499. hour(input : number) : Dayuts
  500. hour(input : number | null = null) : any {
  501. if (input == null) return this.$H
  502. return this.set(H, input)
  503. }
  504. /**
  505. * 获取或设置分钟。
  506. * @param {number | null} [input] - 要设置的分钟(0-59)。
  507. * @returns {number | Dayuts} 分钟或 Dayuts 实例。
  508. */
  509. minute() : number
  510. minute(input : number) : Dayuts
  511. minute(input : number | null = null) : any {
  512. if (input == null) return this.$m
  513. return this.set(MIN, input)
  514. }
  515. /**
  516. * 获取或设置秒。
  517. * @param {number | null} [input] - 要设置的秒(0-59)。
  518. * @returns {number | Dayuts} 秒或 Dayuts 实例。
  519. */
  520. second() : number
  521. second(input : number) : Dayuts
  522. second(input : number | null = null) : any {
  523. if (input == null) return this.$s
  524. return this.set(S, input)
  525. }
  526. /**
  527. * 获取或设置毫秒。
  528. * @param {number | null} [input] - 要设置的毫秒(0-999)。
  529. * @returns {number | Dayuts} 毫秒或 Dayuts 实例。
  530. */
  531. millisecond() : number
  532. millisecond(input : number) : Dayuts
  533. millisecond(input : number | null = null) : any {
  534. if (input == null) return this.$ms
  535. return this.set(MS, input)
  536. }
  537. /**
  538. * 在当前 Dayuts 实例上添加指定的时间长度。
  539. * @param {number} number - 要添加的时间长度。
  540. * @param {string} units - 要添加的时间单位(例如,“years”,“months”,“days”等)。
  541. * @returns {Dayuts} 更新的 Dayuts 实例。
  542. */
  543. add(number : number, units : DayutsUnit) : Dayuts {
  544. const unit = prettyUnit(units)
  545. // 创建一个新的 Dayuts 实例,并根据给定的 n 值设置日期。
  546. // n 值乘以 number 参数,然后加到当前日期上,以设置新的日期。
  547. const instanceFactorySet = (n : number) : Dayuts => {
  548. // 创建一个新的 Dayuts 实例,它是当前实例的副本
  549. const d = dayuts(this)
  550. // 设置新的日期并返回更新后的 Dayuts 实例
  551. return d.date(d.date() + Math.round(n * number)) //Utils.w(d.date(d.date() + Math.round(n * number)), this)
  552. }
  553. if (unit == M) {
  554. return this.set(M, this.$M + number)
  555. }
  556. if (unit == Y) {
  557. return this.set(Y, this.$y + number)
  558. }
  559. if (unit == D) {
  560. return instanceFactorySet(1)
  561. }
  562. if (unit == W) {
  563. return instanceFactorySet(7)
  564. }
  565. const steps = new Map<string, number>([
  566. [MIN, MILLISECONDS_A_MINUTE],
  567. [H, MILLISECONDS_A_HOUR],
  568. [S, MILLISECONDS_A_SECOND],
  569. ])
  570. const step = steps.get(unit) ?? 1;
  571. const nextTimeStamp = this.$d.getTime() + (number * step)
  572. return wrapper(nextTimeStamp, this)
  573. }
  574. /**
  575. * 从当前 Dayuts 实例中减去指定的时间。
  576. * @param {number} number - 要减去的时间。
  577. * @param {string} units - 要减去的时间单位(例如,“years”,“months”,“days”等)。
  578. * @returns {Dayuts} 更新的 Dayuts 实例。
  579. */
  580. subtract(number : number, units : DayutsUnit) : Dayuts {
  581. // 通过将 number 乘以 -1,然后调用 add 方法来实现减法操作
  582. return this.add(number * -1, units);
  583. }
  584. /**
  585. * 日期格式化
  586. * @param {string} formatStr - 格式化字符串,包含各种格式化占位符(例如,“YYYY-MM-DD”,“HH:mm:ss”等)。
  587. * @returns {string} 格式化后的日期字符串。
  588. */
  589. format(formatStr : string | null = null) : string {
  590. const locale = this.$locale();
  591. if (!this.isValid()) return INVALID_DATE_STRING // locale.invalidDate || INVALID_DATE_STRING;
  592. const str = formatStr ?? FORMAT_DEFAULT;
  593. // @ts-ignore
  594. const zoneStr = padZoneStr(this);
  595. const { $H, $m, $M } = this;
  596. const { weekdays, months, meridiem } = locale;
  597. /**
  598. * 从给定的数组中获取缩写或完整的字符串。
  599. * @param {Array} arr - 包含缩写字符串的数组。
  600. * @param {number} index - 数组中要获取的元素的索引。
  601. * @param {Array} full - 包含完整字符串的数组。
  602. * @param {number} length - 要返回的字符串的长度。
  603. * @returns {string} 缩写或完整的字符串。
  604. */
  605. function getShort(arr : string[] | null, index : number, full : string[] = [], length : number = 0) : string {
  606. if (arr != null && arr.length >= index) {
  607. return arr[index]
  608. } else if (full.length >= index) {
  609. return full[index].slice(0, length)
  610. }
  611. return ''
  612. };
  613. /**
  614. * 获取12小时制的小时数。
  615. * @param {number} num - 小时数的位数。
  616. * @returns {string} 12小时制的小时数字符串。
  617. */
  618. const get$H = (num : number) : string => padStart(($H % 12 == 0 ? 12 : $H % 12).toString(), num, '0')
  619. /**
  620. * 获取上午或下午的字符串表示。
  621. * @param {number} hour - 小时数。
  622. * @param {number} minute - 分钟数。
  623. * @param {boolean} isLowercase - 是否返回小写字符串。
  624. * @returns {string} 上午或下午的字符串表示。
  625. */
  626. const meridiemFunc = meridiem ?? ((hour : number, _ : number, isLowercase : boolean) : string => {
  627. const m = (hour < 12 ? 'AM' : 'PM');
  628. return isLowercase ? m.toLowerCase() : m;
  629. });
  630. // #ifdef APP-ANDROID
  631. return str.replace('YYYY', padStart(this.$y.toString(), 4, '0'))
  632. .replace('YY', (this.$y).toString().slice(-2))
  633. .replace('MMMM', getShort(months, $M))
  634. .replace('MM', padStart(($M + 1).toString(), 2, '0'))
  635. .replace('M', ($M + 1).toString())
  636. .replace('DD', padStart(this.$D.toString(), 2, '0'))
  637. .replace('D', this.$D.toString())
  638. .replace('dddd', weekdays[this.$W])
  639. .replace('ddd', getShort(locale.weekdaysShort, this.$W, weekdays, 3))
  640. .replace('dd', getShort(locale.weekdaysMin, this.$W, weekdays, 2))
  641. .replace('d', this.$W.toString())
  642. .replace('HH', padStart($H.toString(), 2, '0'))
  643. .replace('H', $H.toString())
  644. .replace('hh', get$H(2))
  645. .replace('h', get$H(1))
  646. .replace('mm', padStart($m.toString(), 2, '0'))
  647. .replace('m', $m.toString())
  648. .replace('ss', padStart(this.$s.toString(), 2, '0'))
  649. .replace('s', this.$s.toString())
  650. .replace('SSS', padStart(this.$ms.toString(), 3, '0'))
  651. .replace('A', meridiemFunc($H, $m, false))
  652. .replace('a', meridiemFunc($H, $m, true))
  653. .replace('Z', zoneStr)
  654. // #endif
  655. // #ifndef APP-ANDROID
  656. const matches = (match : string) : string | null => {
  657. if (match == 'YY') {
  658. return (this.$y).toString().slice(-2);
  659. } else if (match == 'YYYY') {
  660. return padStart(this.$y.toString(), 4, '0');
  661. } else if (match == 'M') {
  662. return ($M + 1).toString();
  663. } else if (match == 'MM') {
  664. return padStart(($M + 1).toString(), 2, '0');
  665. } else if (match == 'MMM') {
  666. return getShort(locale.monthsShort, $M, months, 3);
  667. } else if (match == 'MMMM') {
  668. return getShort(months, $M);
  669. } else if (match == 'D') {
  670. return this.$D.toString();
  671. } else if (match == 'DD') {
  672. return padStart(this.$D.toString(), 2, '0');
  673. } else if (match == 'd') {
  674. return this.$W.toString();
  675. } else if (match == 'dd') {
  676. return getShort(locale.weekdaysMin, this.$W, weekdays, 2);
  677. } else if (match == 'ddd') {
  678. return getShort(locale.weekdaysShort, this.$W, weekdays, 3);
  679. } else if (match == 'dddd') {
  680. return weekdays[this.$W];
  681. } else if (match == 'H') {
  682. return $H.toString();
  683. } else if (match == 'HH') {
  684. return padStart($H.toString(), 2, '0');
  685. } else if (match == 'h') {
  686. return get$H(1);
  687. } else if (match == 'hh') {
  688. return get$H(2);
  689. } else if (match == 'a') {
  690. return meridiemFunc($H, $m, true);
  691. } else if (match == 'A') {
  692. return meridiemFunc($H, $m, false);
  693. } else if (match == 'm') {
  694. return $m.toString();
  695. } else if (match == 'mm') {
  696. return padStart($m.toString(), 2, '0');
  697. } else if (match == 's') {
  698. return this.$s.toString();
  699. } else if (match == 'ss') {
  700. return padStart(this.$s.toString(), 2, '0');
  701. } else if (match == 'SSS') {
  702. return padStart(this.$ms.toString(), 3, '0');
  703. } else if (match == 'Z') {
  704. return zoneStr; // 'ZZ' logic below
  705. }
  706. return null;
  707. };
  708. return str.replace(REGEX_FORMAT, (match : string, $1 : string, offset : number, string : string) : string => {
  709. return $1 ?? matches(match) ?? zoneStr.replace(':', '')
  710. })
  711. // #endif
  712. }
  713. /**
  714. * 获取 Dayuts 实例的 UTC 偏移量(以分钟为单位)。
  715. * @returns {number} UTC 偏移量(以分钟为单位)。
  716. */
  717. utcOffset() : number {
  718. // Because a bug at FF24, we're rounding the timezone offset around 15 minutes
  719. // https://github.com/moment/moment/pull/1871
  720. // #ifndef APP-ANDROID || APP-IOS
  721. return -Math.round(this.$d.getTimezoneOffset() / 15) * 15
  722. // #endif
  723. // #ifdef APP-ANDROID || APP-IOS
  724. return 0
  725. // #endif
  726. }
  727. /**
  728. * 计算两个日期之间的差值
  729. * @param {string|number|Date|Dayuts} input - 要比较的日期
  730. * @param {string} units - 要计算的时间单位,如 'years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds'
  731. * @param {boolean} float - 是否返回浮点数结果
  732. * @returns {number} 返回两个日期之间的差值
  733. */
  734. diff(input : string) : number
  735. diff(input : number) : number
  736. diff(input : Date) : number
  737. diff(input : Dayuts) : number
  738. diff(input : UTSJSONObject) : number
  739. diff(input : string, units : DayutsUnit) : number
  740. diff(input : number, units : DayutsUnit) : number
  741. diff(input : Date, units : DayutsUnit) : number
  742. diff(input : Dayuts, units : DayutsUnit) : number
  743. diff(input : UTSJSONObject, units : DayutsUnit) : number
  744. diff(input : string, units : DayutsUnit, float : boolean) : number
  745. diff(input : number, units : DayutsUnit, float : boolean) : number
  746. diff(input : Date, units : DayutsUnit, float : boolean) : number
  747. diff(input : Dayuts, units : DayutsUnit, float : boolean) : number
  748. diff(input : UTSJSONObject, units : DayutsUnit, float : boolean) : number
  749. diff(input : any, units : DayutsUnit = 'millisecond', float : boolean = false) : number {
  750. const unit = prettyUnit(units)
  751. const that = dayuts(input)
  752. const zoneDelta = (that.utcOffset() - this.utcOffset()) * MILLISECONDS_A_MINUTE
  753. const diff = this.valueOf() - that.valueOf()
  754. // @ts-ignore
  755. const getMonth = () : number => monthDiff(this, that)
  756. let result : number;
  757. switch (unit) {
  758. case Y:
  759. result = getMonth() / 12
  760. break
  761. case M:
  762. result = getMonth()
  763. break
  764. case Q:
  765. result = getMonth() / 3
  766. break
  767. case W:
  768. result = (diff - zoneDelta) / MILLISECONDS_A_WEEK
  769. break
  770. case D:
  771. result = (diff - zoneDelta) / MILLISECONDS_A_DAY
  772. break
  773. case H:
  774. result = diff / MILLISECONDS_A_HOUR
  775. break
  776. case MIN:
  777. result = diff / MILLISECONDS_A_MINUTE
  778. break
  779. case S:
  780. result = diff / MILLISECONDS_A_SECOND
  781. break
  782. default:
  783. result = diff // milliseconds
  784. break
  785. }
  786. return float ? result : absFloor(result)
  787. }
  788. /**
  789. * 将当前 Dayuts 对象转换为原生 Date 对象。
  790. *
  791. * @returns {Date} 返回一个表示当前日期的原生 Date 对象。
  792. */
  793. toDate() : Date {
  794. return new Date(this.valueOf())
  795. }
  796. /**
  797. * 将 Moment 对象转换为 JSON 字符串
  798. * @returns {string | null} 如果 Moment 对象有效,则返回 ISO 8601 格式的字符串,否则返回 null
  799. */
  800. toJSON() : string | null {
  801. return this.isValid() ? this.toISOString() : null;
  802. }
  803. /**
  804. * 将 Moment 对象转换为 ISO 8601 格式的字符串
  805. * @returns {string} 返回 ISO 8601 格式的字符串
  806. */
  807. toISOString() : string {
  808. // #ifndef APP-ANDROID || APP-IOS
  809. return this.$d.toISOString();
  810. // #endif
  811. // #ifdef APP-ANDROID || APP-IOS
  812. return this.$d.toString();
  813. // #endif
  814. }
  815. toObject() : DayutsObject {
  816. return {
  817. years: this.$y,
  818. months: this.$M,
  819. date: this.$D,
  820. hours: this.$H,
  821. minutes: this.$m,
  822. seconds: this.$s,
  823. milliseconds: this.$ms
  824. } as DayutsObject
  825. }
  826. toArray() : number[] {
  827. return [
  828. this.$y,
  829. this.$M,
  830. this.$D,
  831. this.$H,
  832. this.$m,
  833. this.$s,
  834. this.$ms
  835. ]
  836. }
  837. /**
  838. * 获取当前日期的毫秒数。
  839. *
  840. * @returns {number} 返回一个表示当前日期的毫秒数。
  841. */
  842. valueOf() : number {
  843. // 使用 Date 对象的 getTime 方法获取当前日期的毫秒数。
  844. return this.$d.getTime()
  845. }
  846. /**
  847. * 获取当前 `dayuts` 对象所在月份的天数。
  848. *
  849. * @returns {number} 返回当前 `dayuts` 对象所在月份的天数。
  850. */
  851. daysInMonth() : number {
  852. return this.endOf(M).$D;
  853. }
  854. /**
  855. * 获取当前日期的区域设置对象。
  856. *
  857. * @returns {Object} 区域设置对象。
  858. */
  859. private $locale() : DayutsLocale { // get locale object
  860. // return Ls.get(this.$L)!
  861. return localeState.locales.get(this.$L)!
  862. }
  863. /**
  864. * 设置或获取 Dayuts 实例的本地化配置
  865. * @param {string|Object} preset - 本地化预设名称或自定义本地化配置对象
  866. * @param {Object} [DayutsLocale] - 可选的自定义本地化配置对象
  867. * @returns {Dayuts|string} 如果设置了本地化配置,则返回一个新的 Dayuts 实例;否则返回当前实例的本地化配置名称
  868. */
  869. locale(preset : string, object : DayutsLocale) : Dayuts
  870. locale(preset : DayutsLocale, object : DayutsLocale) : Dayuts
  871. locale(preset : any, object : DayutsLocale | null = null) : Dayuts {
  872. // if (!preset) return this.$L
  873. const that = this.clone()
  874. const nextLocaleName = parseLocale(preset, object, true)
  875. if (nextLocaleName != null) that.$L = nextLocaleName
  876. return that
  877. }
  878. clone() : Dayuts {
  879. return wrapper(this.$d.getTime(), this)
  880. }
  881. /**
  882. * 返回当前 dayuts 对象的 UTC 字符串表示。
  883. *
  884. * @returns {string} 当前 dayuts 对象的 UTC 字符串表示。
  885. */
  886. // #ifdef APP-ANDROID
  887. override toString() : string {
  888. // return this.$d.toUTCString();
  889. // const locale = localeState.locales.get('en')!
  890. // const weekday = locale.weekdays[this.$d.getDay()].substring(0,3);
  891. // const month = locale.months[this.$d.getMonth()].substring(0,3)
  892. // const day = `${this.$D}`.padStart(2, '0');
  893. // const hours = `${this.$H}`.padStart(2, '0');
  894. // const minutes = `${this.$m}`.padStart(2, '0');
  895. // const seconds = `${this.$s}`.padStart(2, '0');
  896. // return `${weekday}, ${day} ${month} ${this.$y} ${hours}:${minutes}:${seconds} GMT`;
  897. return this.$d.toString();
  898. }
  899. // #endif
  900. // #ifndef APP-ANDROID
  901. toString() : string {
  902. // return this.$d.toUTCString();
  903. return this.$d.toString();
  904. }
  905. // #endif
  906. /**
  907. * 计算给定日期在当年的第几天,或者设置给定日期为当年的第几天。
  908. * @param {number} [input] - 如果提供了输入值,则将日期设置为当年的第几天。如果没有提供输入值,则返回当前日期在当年的第几天。
  909. * @returns {number} 如果提供了输入值,则返回调整后的日期。如果没有提供输入值,则返回当前日期在当年的第几天。
  910. */
  911. dayOfYear() : number
  912. dayOfYear(input : number) : Dayuts
  913. dayOfYear(input : number | null = null) : any {
  914. const dayOfYear = Math.round((this.startOf('day').valueOf() - this.startOf('year').valueOf()) / 864e5) + 1
  915. return input == null ? dayOfYear : this.add(input - dayOfYear, 'day')
  916. }
  917. /**
  918. * 根据输入的时间计算与当前时间的相对时间差,并以指定的格式返回。
  919. * @param {Date|number|string} input - 输入的时间,可以是Date对象、时间戳或符合Date.parse()方法的字符串
  920. * @param {boolean} withoutSuffix - 是否省略“未来”或“过去”的后缀
  921. * @param {Object} instance - 当前时间的实例
  922. * @param {boolean} isFrom - 是否从输入时间计算到当前时间
  923. * @param {Function} postFormat - 格式化绝对值后的结果的函数
  924. * @returns {string} 相对时间差的格式化字符串
  925. */
  926. // postFormat
  927. fromToBase(input : string, withoutSuffix : boolean, instance : Dayuts, isFrom : boolean) : string
  928. fromToBase(input : number, withoutSuffix : boolean, instance : Dayuts, isFrom : boolean) : string
  929. fromToBase(input : Date, withoutSuffix : boolean, instance : Dayuts, isFrom : boolean) : string
  930. fromToBase(input : Dayuts, withoutSuffix : boolean, instance : Dayuts, isFrom : boolean) : string
  931. fromToBase(input : UTSJSONObject, withoutSuffix : boolean, instance : Dayuts, isFrom : boolean) : string
  932. fromToBase(input : any, withoutSuffix : boolean, instance : Dayuts, isFrom : boolean) : string {
  933. const relObj = localeState.locales.get('en')?.relativeTime
  934. const loc = instance.$locale().relativeTime ?? relObj
  935. if (loc == null) return '';
  936. const T : Threshold[] = [
  937. { l: 's', r: 44, d: S },
  938. { l: 'm', r: 89 },
  939. { l: 'mm', r: 44, d: MIN },
  940. { l: 'h', r: 89 },
  941. { l: 'hh', r: 21, d: H },
  942. { l: 'd', r: 35 },
  943. { l: 'dd', r: 25, d: D },
  944. { l: 'M', r: 45 },
  945. { l: 'MM', r: 10, d: M },
  946. { l: 'y', r: 17 },
  947. { l: 'yy', d: Y }
  948. ]
  949. const Tl = T.length
  950. let result : number = 0;
  951. let out : string = '';
  952. let isFuture : boolean = false
  953. for (let i = 0; i < Tl; i += 1) {
  954. let t = T[i]
  955. if (t.d != null) {
  956. result = isFrom
  957. ? dayuts(input).diff(instance, t.d!, true)
  958. : instance.diff(input, t.d!, true)
  959. }
  960. let abs = Math.round(Math.abs(result))
  961. isFuture = result > 0
  962. if (t.r == null || t.r != null && abs <= t.r!) {
  963. if (abs <= 1 && i > 0) t = T[i - 1] // 1 minutes -> a minute, 0 seconds -> 0 second
  964. const format = loc[t.l]
  965. // if (postFormat) {
  966. // abs = postFormat(`${abs}`)
  967. // }
  968. if (typeof format == 'string') {
  969. out = (format as string).replace('%d', abs.toString())
  970. }
  971. // else {
  972. // out = format(abs, withoutSuffix, t.l!, isFuture)
  973. // }
  974. break
  975. }
  976. }
  977. if (withoutSuffix) return out
  978. const pastOrFuture = isFuture ? loc.future : loc.past
  979. // if (typeof pastOrFuture == 'function') {
  980. // return pastOrFuture(out)
  981. // }
  982. return pastOrFuture.replace('%s', out)
  983. }
  984. /**
  985. * 相对指定时间(后)。
  986. * @param {string|number|Date|Dayuts|UTSJSONObject} input - 输入的时间,可以是字符串、数字(时间戳)、Date对象、Dayuts对象或UTSJSONObject。
  987. * @param {boolean} withoutSuffix - 是否省略“未来”或“过去”的后缀。
  988. * @returns {string} 相对时间差的格式化字符串。
  989. */
  990. to(input : string) : string
  991. to(input : number) : string
  992. to(input : Date) : string
  993. to(input : Dayuts) : string
  994. to(input : UTSJSONObject) : string
  995. to(input : string, withoutSuffix : boolean) : string
  996. to(input : number, withoutSuffix : boolean) : string
  997. to(input : Date, withoutSuffix : boolean) : string
  998. to(input : Dayuts, withoutSuffix : boolean) : string
  999. to(input : UTSJSONObject, withoutSuffix : boolean) : string
  1000. to(input : any, withoutSuffix : boolean = false) : string {
  1001. return this.fromToBase(input, withoutSuffix, this, true)
  1002. }
  1003. /**
  1004. * 将当前时间转换为与输入时间的相对时间差,并以指定的格式返回。
  1005. * @param {string|number|Date|Dayuts|UTSJSONObject} input - 输入的时间,可以是字符串、数字(时间戳)、Date对象、Dayuts对象或UTSJSONObject。
  1006. * @param {boolean} withoutSuffix - 是否省略“未来”或“过去”的后缀。
  1007. * @returns {string} 相对时间差的格式化字符串。
  1008. */
  1009. from(input : string) : string
  1010. from(input : number) : string
  1011. from(input : Date) : string
  1012. from(input : Dayuts) : string
  1013. from(input : UTSJSONObject) : string
  1014. from(input : string, withoutSuffix : boolean) : string
  1015. from(input : number, withoutSuffix : boolean) : string
  1016. from(input : Date, withoutSuffix : boolean) : string
  1017. from(input : Dayuts, withoutSuffix : boolean) : string
  1018. from(input : UTSJSONObject, withoutSuffix : boolean) : string
  1019. from(input : any, withoutSuffix : boolean = false) : string {
  1020. return this.fromToBase(input, withoutSuffix, this, false)
  1021. }
  1022. /**
  1023. * 获取当前时间与实例时间的相对时间差,并以指定的格式返回。
  1024. * @param {boolean} withoutSuffix - 是否省略“未来”或“过去”的后缀。
  1025. * @returns {string} 相对时间差的格式化字符串。
  1026. */
  1027. toNow() : string
  1028. toNow(withoutSuffix : boolean = false) : string {
  1029. return this.to(dayuts(), withoutSuffix)
  1030. }
  1031. /**
  1032. * 获取实例时间与当前时间的相对时间差,并以指定的格式返回。
  1033. * @param {boolean} withoutSuffix - 是否省略“未来”或“过去”的后缀。
  1034. * @returns {string} 相对时间差的格式化字符串。
  1035. */
  1036. fromNow() : string
  1037. fromNow(withoutSuffix : boolean = false) : string {
  1038. return this.from(dayuts(), withoutSuffix)
  1039. }
  1040. }
  1041. function dayuts() : Dayuts;
  1042. function dayuts(date : string) : Dayuts;
  1043. function dayuts(date : any[]) : Dayuts;
  1044. function dayuts(date : number) : Dayuts;
  1045. function dayuts(date : UTSJSONObject) : Dayuts;
  1046. function dayuts(date : Date) : Dayuts;
  1047. function dayuts(date : Dayuts) : Dayuts;
  1048. // #ifndef APP-ANDROID || APP-IOS
  1049. function dayuts(date : any | null, format : string) : Dayuts;
  1050. function dayuts(date : any | null, format : string | null, locale : string | null) : Dayuts;
  1051. // #endif
  1052. function dayuts(date : any | null = null, format : string | null = null, locale : string | null = null) : Dayuts {
  1053. if (date != null && date instanceof Dayuts) return date.clone()
  1054. return new Dayuts({
  1055. date,
  1056. format,
  1057. locale
  1058. } as DayutsConfig)
  1059. }
  1060. /**
  1061. * 判断给定的对象是否为Dayuts实例
  1062. * @param {(any | null)} date - 输入的对象
  1063. * @returns {boolean} - 如果给定的对象是Dayuts实例,则返回true,否则返回false
  1064. */
  1065. function isDayuts(date : any | null = null) : boolean {
  1066. return date instanceof Dayuts
  1067. }
  1068. export {
  1069. dayuts,
  1070. isDayuts
  1071. }