# @angular-stdlib/date

> Angular standard library for dates

Latest version **0.3.2** (published 2021-05-17) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @angular-stdlib/date
pnpm add @angular-stdlib/date
yarn add @angular-stdlib/date
bun add @angular-stdlib/date
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2021-05-17 |
| First published | 2019-12-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 228.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | magnasilvar, kongrooparadox, mazizi |
| Keywords | angular, stdlib, date, validator, validation, iso8601, interceptor |

## Links

- npm: https://www.npmjs.com/package/@angular-stdlib/date
- Repository: https://github.com/PACK-Solutions/angular-stdlib/tree/master/projects/angular-stdlib/date
- Homepage: https://github.com/PACK-Solutions/angular-stdlib/tree/master/projects/angular-stdlib/date#readme
- npm.io page: https://npm.io/package/@angular-stdlib/date

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^1.9.0

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 0.3.2 (latest) — 2021-05-17
- 0.3.1 — 2020-01-22
- 0.3.0 — 2020-01-04
- 0.2.1 — 2019-12-22
- 0.2.0 — 2019-12-16

## README

[![npm version](https://badge.fury.io/js/%40angular-stdlib%2Fdate.svg)](https://badge.fury.io/js/%40angular-stdlib%2Fdate)

# @angular-stdlib/date

> WARNING - Library is under development and may have API breaking changes for versions prior to 1.0.0.

## Installation

| Angular | @angular-stdlib/date |
| ------- | -------------------- |
| 7 | 0.x |

Install `@angular-stdlib/date` from `npm`:
```bash
npm install @angular-stdlib/date --save
```

## Features

### DateHttpInterceptor

**@angular-stdlib/date** provides an interceptor that intercepts HTTP calls and converts from JSON response body : [ISO 8601](https://www.iso.org/fr/iso-8601-date-and-time-format.html) date and datetime strings to native Date object.

To automatically use this interceptor, simply provide `DateHttpInterceptor` as HTTP_INTERCEPTORS with `multi` flag enabled to keep Angular behavior:

```typescript
import { DateHttpInterceptor } from '@angular-stdlib/date';

@NgModule({
  declarations: [AppComponent],
  providers: [
    { provide: HTTP_INTERCEPTORS, useClass: DateHttpInterceptor, multi: true}
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
```

> NOTE - `DateHttpInterceptor` for projects using dependencies like [ngx-bootstrap/datepicker](https://github.com/valor-software/ngx-bootstrap/tree/development/src/datepicker), this one does not handle "ISO string dates" unlike Angular.<br>
> Linked to this open issue: https://github.com/valor-software/ngx-bootstrap/issues/4487

### DateFormatter

`DateFormatter` is a static class that provides :
* **ISO_DATE** string format (*yyyy-MM-dd*) to use with Angular [DatePipe](https://angular.io/api/common/DatePipe).
* `toFakeSerializedDate(date: Date)` method : transforms the param date in the same way as it would be serialized :
   * Serialization uses `JSON.stringify` method which use the `toJSON()` date method which transforms the date into UTC Z.
   * Only use when you have to serialize dates and you don't want serialization to apply zero UTC offset transformation.

### DateValidators

Provides a set of built-in validators for Dates that can be used by form controls :
* **date** : Validator that requires the control's value to be a `Date` :
  ```typescript
  const control = new FormControl(123, DateValidators.date);
  console.log(control.errors); // {date: true}
  ```
* **beforeDate** : Validator that requires the control's value to be prior than the date in parameter :
  ```typescript
  const control = new FormControl(new Date('2020-01-01'), DateValidators.beforeDate(new Date('2019-01-01')));
  console.log(control.errors); // {beforeDate: true}
  ```
  With optional `dateName` parameter :
  ```typescript
  const control = new FormControl(new Date('2020-01-01'), DateValidators.beforeDate(new Date('2019-01-01'), 'birthdate'));
  console.log(control.errors); // {beforeDate: {dateName: 'birthdate'}}
  ```
* **notBeforeDate** : Validator that requires the control's value not to be prior than the date in parameter :
  ```typescript
  const control = new FormControl(new Date('2019-01-01'), DateValidators.notBeforeDate(new Date('2020-01-01')));
  console.log(control.errors); // {notBeforeDate: true}
  ```
  With optional `dateName` parameter :
  ```typescript
  const control = new FormControl(new Date('2019-01-01'), DateValidators.notBeforeDate(new Date('2020-01-01'), 'birthdate'));
  console.log(control.errors); // {notBeforeDate: {dateName: 'birthdate'}}
  ```
* **afterDate** : Validator that requires the control's value to be later than the date in parameter :
  ```typescript
  const control = new FormControl(new Date('2019-01-01'), DateValidators.afterDate(new Date('2020-01-01')));
  console.log(control.errors); // {afterDate: true}
  ```
  With optional `dateName` parameter :
  ```typescript
  const control = new FormControl(new Date('2019-01-01'), DateValidators.afterDate(new Date('2020-01-01'), 'birthdate'));
  console.log(control.errors); // {afterDate: {dateName: 'birthdate'}}
  ```
* **notAfterDate** : Validator that requires the control's value not to be later than the date in parameter :
  ```typescript
  const control = new FormControl(new Date('2020-01-01'), DateValidators.notAfterDate(new Date('2019-01-01')));
  console.log(control.errors); // {notAfterDate: true}
  ```
  With optional `dateName` parameter :
  ```typescript
  const control = new FormControl(new Date('2020-01-01'), DateValidators.notAfterDate(new Date('2019-01-01'), 'birthdate'));
  console.log(control.errors); // {notAfterDate: {dateName: 'birthdate'}}
  ```

> NOTE - For **beforeDate**, **notBeforeDate**, **afterDate** and **notAfterDate**, if control's value is not a `Date`, validation will always be a success.<br>
> To check date format, combine previous validator with `DateValidators.date`.

---
_Source: https://npm.io/package/@angular-stdlib/date · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
