npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

angular-date-value-accessor

v3.0.0

Published

A set of ControlValueAccessors for Angular to work with Browser's native date input elements. Now you can use <input type='date'> with template-driven and reactive forms!

Downloads

3,589

Readme

DateValueAccessor for Angular

NPM version Tests

A set of ControlValueAccessors for Angular to work with Browser's native date input elements. Now you can use <input type="date"> directly with two-way data bindings (ngModel) as well as with reactive forms (formControlName/formControl). Choose freely between date objects and ISO-formatted strings.

Demo

Here you can see the DateValueAccessor - the binding works! Changes to the input field are propagated to the model.

Example: works

And here you can see the LocalDateValueAccessor ⭐️. Please notice how the date is adjusted due to the German time zone (UTC+1) and how the time offset works.

Example: works

And this shows a not working form field (the default behaviour). Changes in the input field are propagated to the model, but unfortunately the date becomes a string which is not very useful for any further processing.

Example: does not work

You can try out a full demo at the following page:
→ http://johanneshoppe.github.io/angular-date-value-accessor/

Installation

Download the package via NPM:

npm install angular-date-value-accessor

UTC Time or Local Time

When working with Dates in Javascript you either operate in UTC or Local Time.

  • UTC has no timezone offset.
  • Local Time depends on the host system time zone and offset.

Javascript Dates support both the UTC and the Local Time representation. Depending on the requirements of your application you can choose from these Value Accessors:

ℹ️ Hint: Most UI component libraries like Angular Material, Kendo Angular, PrimeNG implement their DatePickers operating in Local Time. The Angular Date Pipe uses the Local Time representation of the Date Object by default, too.

Installation & Usage

You have to explicitly opt-in by adding one of these attribute directives to a HTML date input control: useValueAsDate, useValueAsLocalDate, useValueAsIso or useValueAsLocalIso.

DateValueAccessor (UTC)

The original DateValueAccessor operates in UTC (Coordinated Universal Time). The HTML date input will use the UTC representation of a given Date Object. When you select a date it will output an UTC date with the time set to 00:00 (UTC).

If you are unsure what to use, use the LocalDateValueAccessor and not the DateValueAccessor. Most users will expect the input field to correlate to their local clock.

Import the standalone directive or the NgModule:

// app.module.ts

import { DateValueAccessor, DateValueAccessorModule } from 'angular-date-value-accessor';

@NgModule({
  imports: [
    DateValueAccessor
    // OR
    DateValueAccessorModule
  ]
})
export class AppModule {}

Now you can apply the useValueAsDate to your date input controls:

<!-- DateValueAccessor (UTC) --->

<input type="date"
       name="myBirthday"
       [(ngModel)]="myBirthday"
       useValueAsDate>

OR

<input type="date"
       formControlName="myBirthday"
       useValueAsDate>

LocalDateValueAccessor (Local Time) ⭐️

The improved LocalDateValueAccessor operates in your Local Time. The HTML date input will use the Local Time representation of a given the Date Object. When you select a date it will output a Local Date with the time set to 00:00 (Local Time).

Import the standalone directive or the NgModule:

// app.module.ts

import { LocalDateValueAccessor, LocalDateValueAccessorModule } from 'angular-date-value-accessor';

@NgModule({
  imports: [
    LocalDateValueAccessor
    // OR
    LocalDateValueAccessorModule
  ]
})
export class AppModule {}

Now you can apply the useValueAsLocalDate to your date input controls:

<!-- LocalDateValueAccessor (Local Time) ⭐️ --->

<input type="date"
       name="myBirthday"
       [(ngModel)]="myBirthday"
       useValueAsLocalDate>

OR

<input type="date"
       formControlName="myBirthday"
       useValueAsLocalDate>

IsoDateValueAccessor (UTC as ISO 8601 string)

This directive gets and sets ISO 8601 formatted date strings in HTML date inputs. The handling of the dates is the same as for the DateValueAccessor.

The IsoDateValueAccessor operates in UTC (Coordinated Universal Time). The HTML date input will use the UTC representation of a given ISO 8601 formatted date string. When you select a date it will output an ISO-formatted string with the time set to 00:00 (UTC).

Import the standalone directive or the NgModule:

// app.module.ts
import { IsoDateValueAccessor, IsoDateValueAccessorModule } from 'angular-date-value-accessor';

@NgModule({
  imports: [
    IsoDateValueAccessor
    // OR
    IsoDateValueAccessorModule
  ]
})
export class AppModule { }

Now you can apply the useValueAsIso to your date input controls:

<!-- IsoDateValueAccessor (UTC as ISO string) --->

<input type="date"
       name="myBirthday"
       [(ngModel)]="myBirthday"
       useValueAsIso>

OR

<input type="date"
       formControlName="myBirthday"
       useValueAsIso>

LocalIsoDateValueAccessor (Local Time as ISO 8601 string)

This directive gets and sets ISO 8601 formatted date strings in HTML date inputs. The handling of the dates is the same as for the LocalDateValueAccessor.

The LocalIsoDateValueAccessor operates in your Local Time. The HTML date input will use the Local Time representation of a given ISO 8601 formatted date string. When you select a date it will output an ISO-formatted string with a time that equals to 00:00 (Local Time). Note: The timezone of the outputted string is always zero UTC offset, as denoted by the suffix "Z".

Import the standalone directive or the NgModule:

// app.module.ts
import { LocalIsoDateValueAccessor, LocalIsoDateValueAccessorModule } from 'angular-date-value-accessor';

@NgModule({
  imports: [
    LocalIsoDateValueAccessor
    // OR
    LocalIsoDateValueAccessorModule
  ]
})
export class AppModule { }

Now you can apply the useValueAsLocalIso to your date input controls:

<!-- LocalIsoDateValueAccessor (Local Time as ISO string) ⭐️ --->

<input type="date"
       name="myBirthday"
       [(ngModel)]="myBirthday"
       useValueAsLocalIso>

OR

<input type="date"
       formControlName="myBirthday"
       useValueAsLocalIso>

License

This code is published under the MIT license.