UNPKG

13.9 kBTypeScriptView Raw
1/// <reference path="./locale/index.d.ts" />
2
3export = dayjs;
4
5declare function dayjs (date?: dayjs.ConfigType): dayjs.Dayjs
6
7declare function dayjs (date?: dayjs.ConfigType, format?: dayjs.OptionType, strict?: boolean): dayjs.Dayjs
8
9declare function dayjs (date?: dayjs.ConfigType, format?: dayjs.OptionType, locale?: string, strict?: boolean): dayjs.Dayjs
10
11declare namespace dayjs {
12 interface ConfigTypeMap {
13 default: string | number | Date | Dayjs | null | undefined
14 }
15
16 export type ConfigType = ConfigTypeMap[keyof ConfigTypeMap]
17
18 export interface FormatObject { locale?: string, format?: string, utc?: boolean }
19
20 export type OptionType = FormatObject | string | string[]
21
22 export type UnitTypeShort = 'd' | 'D' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms'
23
24 export type UnitTypeLong = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' | 'date'
25
26 export type UnitTypeLongPlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates'
27
28 export type UnitType = UnitTypeLong | UnitTypeLongPlural | UnitTypeShort;
29
30 export type OpUnitType = UnitType | "week" | "weeks" | 'w';
31 export type QUnitType = UnitType | "quarter" | "quarters" | 'Q';
32 export type ManipulateType = Exclude<OpUnitType, 'date' | 'dates'>;
33 class Dayjs {
34 constructor (config?: ConfigType)
35 /**
36 * All Day.js objects are immutable. Still, `dayjs#clone` can create a clone of the current object if you need one.
37 * ```
38 * dayjs().clone()// => Dayjs
39 * dayjs(dayjs('2019-01-25')) // passing a Dayjs object to a constructor will also clone it
40 * ```
41 * Docs: https://day.js.org/docs/en/parse/dayjs-clone
42 */
43 clone(): Dayjs
44 /**
45 * This returns a `boolean` indicating whether the Day.js object contains a valid date or not.
46 * ```
47 * dayjs().isValid()// => boolean
48 * ```
49 * Docs: https://day.js.org/docs/en/parse/is-valid
50 */
51 isValid(): boolean
52 /**
53 * Get the year.
54 * ```
55 * dayjs().year()// => 2020
56 * ```
57 * Docs: https://day.js.org/docs/en/get-set/year
58 */
59 year(): number
60 /**
61 * Set the year.
62 * ```
63 * dayjs().year(2000)// => Dayjs
64 * ```
65 * Docs: https://day.js.org/docs/en/get-set/year
66 */
67 year(value: number): Dayjs
68 /**
69 * Get the month.
70 *
71 * Months are zero indexed, so January is month 0.
72 * ```
73 * dayjs().month()// => 0-11
74 * ```
75 * Docs: https://day.js.org/docs/en/get-set/month
76 */
77 month(): number
78 /**
79 * Set the month.
80 *
81 * Months are zero indexed, so January is month 0.
82 *
83 * Accepts numbers from 0 to 11. If the range is exceeded, it will bubble up to the next year.
84 * ```
85 * dayjs().month(0)// => Dayjs
86 * ```
87 * Docs: https://day.js.org/docs/en/get-set/month
88 */
89 month(value: number): Dayjs
90 /**
91 * Get the date of the month.
92 * ```
93 * dayjs().date()// => 1-31
94 * ```
95 * Docs: https://day.js.org/docs/en/get-set/date
96 */
97 date(): number
98 /**
99 * Set the date of the month.
100 *
101 * Accepts numbers from 1 to 31. If the range is exceeded, it will bubble up to the next months.
102 * ```
103 * dayjs().date(1)// => Dayjs
104 * ```
105 * Docs: https://day.js.org/docs/en/get-set/date
106 */
107 date(value: number): Dayjs
108 /**
109 * Get the day of the week.
110 *
111 * Returns numbers from 0 (Sunday) to 6 (Saturday).
112 * ```
113 * dayjs().day()// 0-6
114 * ```
115 * Docs: https://day.js.org/docs/en/get-set/day
116 */
117 day(): 0 | 1 | 2 | 3 | 4 | 5 | 6
118 /**
119 * Set the day of the week.
120 *
121 * Accepts numbers from 0 (Sunday) to 6 (Saturday). If the range is exceeded, it will bubble up to next weeks.
122 * ```
123 * dayjs().day(0)// => Dayjs
124 * ```
125 * Docs: https://day.js.org/docs/en/get-set/day
126 */
127 day(value: number): Dayjs
128 /**
129 * Get the hour.
130 * ```
131 * dayjs().hour()// => 0-23
132 * ```
133 * Docs: https://day.js.org/docs/en/get-set/hour
134 */
135 hour(): number
136 /**
137 * Set the hour.
138 *
139 * Accepts numbers from 0 to 23. If the range is exceeded, it will bubble up to the next day.
140 * ```
141 * dayjs().hour(12)// => Dayjs
142 * ```
143 * Docs: https://day.js.org/docs/en/get-set/hour
144 */
145 hour(value: number): Dayjs
146 /**
147 * Get the minutes.
148 * ```
149 * dayjs().minute()// => 0-59
150 * ```
151 * Docs: https://day.js.org/docs/en/get-set/minute
152 */
153 minute(): number
154 /**
155 * Set the minutes.
156 *
157 * Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the next hour.
158 * ```
159 * dayjs().minute(59)// => Dayjs
160 * ```
161 * Docs: https://day.js.org/docs/en/get-set/minute
162 */
163 minute(value: number): Dayjs
164 /**
165 * Get the seconds.
166 * ```
167 * dayjs().second()// => 0-59
168 * ```
169 * Docs: https://day.js.org/docs/en/get-set/second
170 */
171 second(): number
172 /**
173 * Set the seconds.
174 *
175 * Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the next minutes.
176 * ```
177 * dayjs().second(1)// Dayjs
178 * ```
179 */
180 second(value: number): Dayjs
181 /**
182 * Get the milliseconds.
183 * ```
184 * dayjs().millisecond()// => 0-999
185 * ```
186 * Docs: https://day.js.org/docs/en/get-set/millisecond
187 */
188 millisecond(): number
189 /**
190 * Set the milliseconds.
191 *
192 * Accepts numbers from 0 to 999. If the range is exceeded, it will bubble up to the next seconds.
193 * ```
194 * dayjs().millisecond(1)// => Dayjs
195 * ```
196 * Docs: https://day.js.org/docs/en/get-set/millisecond
197 */
198 millisecond(value: number): Dayjs
199 /**
200 * Generic setter, accepting unit as first argument, and value as second, returns a new instance with the applied changes.
201 *
202 * In general:
203 * ```
204 * dayjs().set(unit, value) === dayjs()[unit](value)
205 * ```
206 * Units are case insensitive, and support plural and short forms.
207 * ```
208 * dayjs().set('date', 1)
209 * dayjs().set('month', 3) // April
210 * dayjs().set('second', 30)
211 * ```
212 * Docs: https://day.js.org/docs/en/get-set/set
213 */
214 set(unit: UnitType, value: number): Dayjs
215 /**
216 * String getter, returns the corresponding information getting from Day.js object.
217 *
218 * In general:
219 * ```
220 * dayjs().get(unit) === dayjs()[unit]()
221 * ```
222 * Units are case insensitive, and support plural and short forms.
223 * ```
224 * dayjs().get('year')
225 * dayjs().get('month') // start 0
226 * dayjs().get('date')
227 * ```
228 * Docs: https://day.js.org/docs/en/get-set/get
229 */
230 get(unit: UnitType): number
231 /**
232 * Returns a cloned Day.js object with a specified amount of time added.
233 * ```
234 * dayjs().add(7, 'day')// => Dayjs
235 * ```
236 * Units are case insensitive, and support plural and short forms.
237 *
238 * Docs: https://day.js.org/docs/en/manipulate/add
239 */
240 add(value: number, unit?: ManipulateType): Dayjs
241 /**
242 * Returns a cloned Day.js object with a specified amount of time subtracted.
243 * ```
244 * dayjs().subtract(7, 'year')// => Dayjs
245 * ```
246 * Units are case insensitive, and support plural and short forms.
247 *
248 * Docs: https://day.js.org/docs/en/manipulate/subtract
249 */
250 subtract(value: number, unit?: ManipulateType): Dayjs
251 /**
252 * Returns a cloned Day.js object and set it to the start of a unit of time.
253 * ```
254 * dayjs().startOf('year')// => Dayjs
255 * ```
256 * Units are case insensitive, and support plural and short forms.
257 *
258 * Docs: https://day.js.org/docs/en/manipulate/start-of
259 */
260 startOf(unit: OpUnitType): Dayjs
261 /**
262 * Returns a cloned Day.js object and set it to the end of a unit of time.
263 * ```
264 * dayjs().endOf('month')// => Dayjs
265 * ```
266 * Units are case insensitive, and support plural and short forms.
267 *
268 * Docs: https://day.js.org/docs/en/manipulate/end-of
269 */
270 endOf(unit: OpUnitType): Dayjs
271 /**
272 * Get the formatted date according to the string of tokens passed in.
273 *
274 * To escape characters, wrap them in square brackets (e.g. [MM]).
275 * ```
276 * dayjs().format()// => current date in ISO8601, without fraction seconds e.g. '2020-04-02T08:02:17-05:00'
277 * dayjs('2019-01-25').format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]')// 'YYYYescape 2019-01-25T00:00:00-02:00Z'
278 * dayjs('2019-01-25').format('DD/MM/YYYY') // '25/01/2019'
279 * ```
280 * Docs: https://day.js.org/docs/en/display/format
281 */
282 format(template?: string): string
283 /**
284 * This indicates the difference between two date-time in the specified unit.
285 *
286 * To get the difference in milliseconds, use `dayjs#diff`
287 * ```
288 * const date1 = dayjs('2019-01-25')
289 * const date2 = dayjs('2018-06-05')
290 * date1.diff(date2) // 20214000000 default milliseconds
291 * date1.diff() // milliseconds to current time
292 * ```
293 *
294 * To get the difference in another unit of measurement, pass that measurement as the second argument.
295 * ```
296 * const date1 = dayjs('2019-01-25')
297 * date1.diff('2018-06-05', 'month') // 7
298 * ```
299 * Units are case insensitive, and support plural and short forms.
300 *
301 * Docs: https://day.js.org/docs/en/display/difference
302 */
303 diff(date?: ConfigType, unit?: QUnitType | OpUnitType, float?: boolean): number
304 /**
305 * This returns the number of **milliseconds** since the Unix Epoch of the Day.js object.
306 * ```
307 * dayjs('2019-01-25').valueOf() // 1548381600000
308 * +dayjs(1548381600000) // 1548381600000
309 * ```
310 * To get a Unix timestamp (the number of seconds since the epoch) from a Day.js object, you should use Unix Timestamp `dayjs#unix()`.
311 *
312 * Docs: https://day.js.org/docs/en/display/unix-timestamp-milliseconds
313 */
314 valueOf(): number
315 /**
316 * This returns the Unix timestamp (the number of **seconds** since the Unix Epoch) of the Day.js object.
317 * ```
318 * dayjs('2019-01-25').unix() // 1548381600
319 * ```
320 * This value is floored to the nearest second, and does not include a milliseconds component.
321 *
322 * Docs: https://day.js.org/docs/en/display/unix-timestamp
323 */
324 unix(): number
325 /**
326 * Get the number of days in the current month.
327 * ```
328 * dayjs('2019-01-25').daysInMonth() // 31
329 * ```
330 * Docs: https://day.js.org/docs/en/display/days-in-month
331 */
332 daysInMonth(): number
333 /**
334 * To get a copy of the native `Date` object parsed from the Day.js object use `dayjs#toDate`.
335 * ```
336 * dayjs('2019-01-25').toDate()// => Date
337 * ```
338 */
339 toDate(): Date
340 /**
341 * To serialize as an ISO 8601 string.
342 * ```
343 * dayjs('2019-01-25').toJSON() // '2019-01-25T02:00:00.000Z'
344 * ```
345 * Docs: https://day.js.org/docs/en/display/as-json
346 */
347 toJSON(): string
348 /**
349 * To format as an ISO 8601 string.
350 * ```
351 * dayjs('2019-01-25').toISOString() // '2019-01-25T02:00:00.000Z'
352 * ```
353 * Docs: https://day.js.org/docs/en/display/as-iso-string
354 */
355 toISOString(): string
356 /**
357 * Returns a string representation of the date.
358 * ```
359 * dayjs('2019-01-25').toString() // 'Fri, 25 Jan 2019 02:00:00 GMT'
360 * ```
361 * Docs: https://day.js.org/docs/en/display/as-string
362 */
363 toString(): string
364 /**
365 * Get the UTC offset in minutes.
366 * ```
367 * dayjs().utcOffset()
368 * ```
369 * Docs: https://day.js.org/docs/en/manipulate/utc-offset
370 */
371 utcOffset(): number
372 /**
373 * This indicates whether the Day.js object is before the other supplied date-time.
374 * ```
375 * dayjs().isBefore(dayjs('2011-01-01')) // default milliseconds
376 * ```
377 * If you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter.
378 * ```
379 * dayjs().isBefore('2011-01-01', 'year')// => boolean
380 * ```
381 * Units are case insensitive, and support plural and short forms.
382 *
383 * Docs: https://day.js.org/docs/en/query/is-before
384 */
385 isBefore(date?: ConfigType, unit?: OpUnitType): boolean
386 /**
387 * This indicates whether the Day.js object is the same as the other supplied date-time.
388 * ```
389 * dayjs().isSame(dayjs('2011-01-01')) // default milliseconds
390 * ```
391 * If you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter.
392 * ```
393 * dayjs().isSame('2011-01-01', 'year')// => boolean
394 * ```
395 * Docs: https://day.js.org/docs/en/query/is-same
396 */
397 isSame(date?: ConfigType, unit?: OpUnitType): boolean
398 /**
399 * This indicates whether the Day.js object is after the other supplied date-time.
400 * ```
401 * dayjs().isAfter(dayjs('2011-01-01')) // default milliseconds
402 * ```
403 * If you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter.
404 * ```
405 * dayjs().isAfter('2011-01-01', 'year')// => boolean
406 * ```
407 * Units are case insensitive, and support plural and short forms.
408 *
409 * Docs: https://day.js.org/docs/en/query/is-after
410 */
411 isAfter(date?: ConfigType, unit?: OpUnitType): boolean
412
413 locale(): string
414
415 locale(preset: string | ILocale, object?: Partial<ILocale>): Dayjs
416 }
417
418 export type PluginFunc<T = unknown> = (option: T, c: typeof Dayjs, d: typeof dayjs) => void
419
420 export function extend<T = unknown>(plugin: PluginFunc<T>, option?: T): Dayjs
421
422 export function locale(preset?: string | ILocale, object?: Partial<ILocale>, isLocal?: boolean): string
423
424 export function isDayjs(d: any): d is Dayjs
425
426 export function unix(t: number): Dayjs
427
428 const Ls : { [key: string] : ILocale }
429}