date-adapter.d-CtKXIxk0.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import { InjectionToken } from '@angular/core';
  2. import { Subject, Observable } from 'rxjs';
  3. /** InjectionToken for datepicker that can be used to override default locale code. */
  4. declare const MAT_DATE_LOCALE: InjectionToken<{}>;
  5. /**
  6. * @docs-private
  7. * @deprecated No longer used, will be removed.
  8. * @breaking-change 21.0.0
  9. */
  10. declare function MAT_DATE_LOCALE_FACTORY(): {};
  11. /** Adapts type `D` to be usable as a date by cdk-based components that work with dates. */
  12. declare abstract class DateAdapter<D, L = any> {
  13. /** The locale to use for all dates. */
  14. protected locale: L;
  15. protected readonly _localeChanges: Subject<void>;
  16. /** A stream that emits when the locale changes. */
  17. readonly localeChanges: Observable<void>;
  18. /**
  19. * Gets the year component of the given date.
  20. * @param date The date to extract the year from.
  21. * @returns The year component.
  22. */
  23. abstract getYear(date: D): number;
  24. /**
  25. * Gets the month component of the given date.
  26. * @param date The date to extract the month from.
  27. * @returns The month component (0-indexed, 0 = January).
  28. */
  29. abstract getMonth(date: D): number;
  30. /**
  31. * Gets the date of the month component of the given date.
  32. * @param date The date to extract the date of the month from.
  33. * @returns The month component (1-indexed, 1 = first of month).
  34. */
  35. abstract getDate(date: D): number;
  36. /**
  37. * Gets the day of the week component of the given date.
  38. * @param date The date to extract the day of the week from.
  39. * @returns The month component (0-indexed, 0 = Sunday).
  40. */
  41. abstract getDayOfWeek(date: D): number;
  42. /**
  43. * Gets a list of names for the months.
  44. * @param style The naming style (e.g. long = 'January', short = 'Jan', narrow = 'J').
  45. * @returns An ordered list of all month names, starting with January.
  46. */
  47. abstract getMonthNames(style: 'long' | 'short' | 'narrow'): string[];
  48. /**
  49. * Gets a list of names for the dates of the month.
  50. * @returns An ordered list of all date of the month names, starting with '1'.
  51. */
  52. abstract getDateNames(): string[];
  53. /**
  54. * Gets a list of names for the days of the week.
  55. * @param style The naming style (e.g. long = 'Sunday', short = 'Sun', narrow = 'S').
  56. * @returns An ordered list of all weekday names, starting with Sunday.
  57. */
  58. abstract getDayOfWeekNames(style: 'long' | 'short' | 'narrow'): string[];
  59. /**
  60. * Gets the name for the year of the given date.
  61. * @param date The date to get the year name for.
  62. * @returns The name of the given year (e.g. '2017').
  63. */
  64. abstract getYearName(date: D): string;
  65. /**
  66. * Gets the first day of the week.
  67. * @returns The first day of the week (0-indexed, 0 = Sunday).
  68. */
  69. abstract getFirstDayOfWeek(): number;
  70. /**
  71. * Gets the number of days in the month of the given date.
  72. * @param date The date whose month should be checked.
  73. * @returns The number of days in the month of the given date.
  74. */
  75. abstract getNumDaysInMonth(date: D): number;
  76. /**
  77. * Clones the given date.
  78. * @param date The date to clone
  79. * @returns A new date equal to the given date.
  80. */
  81. abstract clone(date: D): D;
  82. /**
  83. * Creates a date with the given year, month, and date. Does not allow over/under-flow of the
  84. * month and date.
  85. * @param year The full year of the date. (e.g. 89 means the year 89, not the year 1989).
  86. * @param month The month of the date (0-indexed, 0 = January). Must be an integer 0 - 11.
  87. * @param date The date of month of the date. Must be an integer 1 - length of the given month.
  88. * @returns The new date, or null if invalid.
  89. */
  90. abstract createDate(year: number, month: number, date: number): D;
  91. /**
  92. * Gets today's date.
  93. * @returns Today's date.
  94. */
  95. abstract today(): D;
  96. /**
  97. * Parses a date from a user-provided value.
  98. * @param value The value to parse.
  99. * @param parseFormat The expected format of the value being parsed
  100. * (type is implementation-dependent).
  101. * @returns The parsed date.
  102. */
  103. abstract parse(value: any, parseFormat: any): D | null;
  104. /**
  105. * Formats a date as a string according to the given format.
  106. * @param date The value to format.
  107. * @param displayFormat The format to use to display the date as a string.
  108. * @returns The formatted date string.
  109. */
  110. abstract format(date: D, displayFormat: any): string;
  111. /**
  112. * Adds the given number of years to the date. Years are counted as if flipping 12 pages on the
  113. * calendar for each year and then finding the closest date in the new month. For example when
  114. * adding 1 year to Feb 29, 2016, the resulting date will be Feb 28, 2017.
  115. * @param date The date to add years to.
  116. * @param years The number of years to add (may be negative).
  117. * @returns A new date equal to the given one with the specified number of years added.
  118. */
  119. abstract addCalendarYears(date: D, years: number): D;
  120. /**
  121. * Adds the given number of months to the date. Months are counted as if flipping a page on the
  122. * calendar for each month and then finding the closest date in the new month. For example when
  123. * adding 1 month to Jan 31, 2017, the resulting date will be Feb 28, 2017.
  124. * @param date The date to add months to.
  125. * @param months The number of months to add (may be negative).
  126. * @returns A new date equal to the given one with the specified number of months added.
  127. */
  128. abstract addCalendarMonths(date: D, months: number): D;
  129. /**
  130. * Adds the given number of days to the date. Days are counted as if moving one cell on the
  131. * calendar for each day.
  132. * @param date The date to add days to.
  133. * @param days The number of days to add (may be negative).
  134. * @returns A new date equal to the given one with the specified number of days added.
  135. */
  136. abstract addCalendarDays(date: D, days: number): D;
  137. /**
  138. * Gets the RFC 3339 compatible string (https://tools.ietf.org/html/rfc3339) for the given date.
  139. * This method is used to generate date strings that are compatible with native HTML attributes
  140. * such as the `min` or `max` attribute of an `<input>`.
  141. * @param date The date to get the ISO date string for.
  142. * @returns The ISO date string date string.
  143. */
  144. abstract toIso8601(date: D): string;
  145. /**
  146. * Checks whether the given object is considered a date instance by this DateAdapter.
  147. * @param obj The object to check
  148. * @returns Whether the object is a date instance.
  149. */
  150. abstract isDateInstance(obj: any): boolean;
  151. /**
  152. * Checks whether the given date is valid.
  153. * @param date The date to check.
  154. * @returns Whether the date is valid.
  155. */
  156. abstract isValid(date: D): boolean;
  157. /**
  158. * Gets date instance that is not valid.
  159. * @returns An invalid date.
  160. */
  161. abstract invalid(): D;
  162. /**
  163. * Sets the time of one date to the time of another.
  164. * @param target Date whose time will be set.
  165. * @param hours New hours to set on the date object.
  166. * @param minutes New minutes to set on the date object.
  167. * @param seconds New seconds to set on the date object.
  168. */
  169. setTime(target: D, hours: number, minutes: number, seconds: number): D;
  170. /**
  171. * Gets the hours component of the given date.
  172. * @param date The date to extract the hours from.
  173. */
  174. getHours(date: D): number;
  175. /**
  176. * Gets the minutes component of the given date.
  177. * @param date The date to extract the minutes from.
  178. */
  179. getMinutes(date: D): number;
  180. /**
  181. * Gets the seconds component of the given date.
  182. * @param date The date to extract the seconds from.
  183. */
  184. getSeconds(date: D): number;
  185. /**
  186. * Parses a date with a specific time from a user-provided value.
  187. * @param value The value to parse.
  188. * @param parseFormat The expected format of the value being parsed
  189. * (type is implementation-dependent).
  190. */
  191. parseTime(value: any, parseFormat: any): D | null;
  192. /**
  193. * Adds an amount of seconds to the specified date.
  194. * @param date Date to which to add the seconds.
  195. * @param amount Amount of seconds to add to the date.
  196. */
  197. addSeconds(date: D, amount: number): D;
  198. /**
  199. * Given a potential date object, returns that same date object if it is
  200. * a valid date, or `null` if it's not a valid date.
  201. * @param obj The object to check.
  202. * @returns A date or `null`.
  203. */
  204. getValidDateOrNull(obj: unknown): D | null;
  205. /**
  206. * Attempts to deserialize a value to a valid date object. This is different from parsing in that
  207. * deserialize should only accept non-ambiguous, locale-independent formats (e.g. a ISO 8601
  208. * string). The default implementation does not allow any deserialization, it simply checks that
  209. * the given value is already a valid date object or null. The `<mat-datepicker>` will call this
  210. * method on all of its `@Input()` properties that accept dates. It is therefore possible to
  211. * support passing values from your backend directly to these properties by overriding this method
  212. * to also deserialize the format used by your backend.
  213. * @param value The value to be deserialized into a date object.
  214. * @returns The deserialized date object, either a valid date, null if the value can be
  215. * deserialized into a null date (e.g. the empty string), or an invalid date.
  216. */
  217. deserialize(value: any): D | null;
  218. /**
  219. * Sets the locale used for all dates.
  220. * @param locale The new locale.
  221. */
  222. setLocale(locale: L): void;
  223. /**
  224. * Compares two dates.
  225. * @param first The first date to compare.
  226. * @param second The second date to compare.
  227. * @returns 0 if the dates are equal, a number less than 0 if the first date is earlier,
  228. * a number greater than 0 if the first date is later.
  229. */
  230. compareDate(first: D, second: D): number;
  231. /**
  232. * Compares the time values of two dates.
  233. * @param first First date to compare.
  234. * @param second Second date to compare.
  235. * @returns 0 if the times are equal, a number less than 0 if the first time is earlier,
  236. * a number greater than 0 if the first time is later.
  237. */
  238. compareTime(first: D, second: D): number;
  239. /**
  240. * Checks if two dates are equal.
  241. * @param first The first date to check.
  242. * @param second The second date to check.
  243. * @returns Whether the two dates are equal.
  244. * Null dates are considered equal to other null dates.
  245. */
  246. sameDate(first: D | null, second: D | null): boolean;
  247. /**
  248. * Checks if the times of two dates are equal.
  249. * @param first The first date to check.
  250. * @param second The second date to check.
  251. * @returns Whether the times of the two dates are equal.
  252. * Null dates are considered equal to other null dates.
  253. */
  254. sameTime(first: D | null, second: D | null): boolean;
  255. /**
  256. * Clamp the given date between min and max dates.
  257. * @param date The date to clamp.
  258. * @param min The minimum value to allow. If null or omitted no min is enforced.
  259. * @param max The maximum value to allow. If null or omitted no max is enforced.
  260. * @returns `min` if `date` is less than `min`, `max` if date is greater than `max`,
  261. * otherwise `date`.
  262. */
  263. clampDate(date: D, min?: D | null, max?: D | null): D;
  264. }
  265. export { DateAdapter as D, MAT_DATE_LOCALE as M, MAT_DATE_LOCALE_FACTORY as a };