index.ts 40 KB

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