Default Date to JSON conversion format in Angular app

Eugeny89

Suppose a component (inside the angular app) having several reactive forms containing datepickers (I'm using mat-datepicker from angular material lib) and other inputs. After a user hit "Submit" button I send the value of forms to backed (with HttpClient.post). The problem is that datepicker fields are serialized as "2020-11-18T22:00:00.000Z" (obviously Date.toJSON() method is called) while backend expects other format.

Note that I'm using formly lib to build my forms, cause set of components on each form may vary. You might not be familiar with formly but anyway set of datepickers may vary as well, so I can't convert datepicker fields directly cause I don't know the exact list of date fields in a place where I send the value of forms.

Is there an elegant solution to my problem? Can't think up something better than monkey-patching Date.prototype.toJSON() or looping through objects sent on the server, check the type of fields and changing field if it's a Date? I can't find a way to set the format of value output by datepicker either in material or in formly.

ibenjelloun

Implementing ControlValueAccessor would be an elegant solution. The idea is to create a date picker component that takes your date format as input and send back your format as output.

For that you just have to create a new component that I would call MatDatepickerWrapperComponent for this example. The template of this component would be nothing more than the material date picker :

<mat-form-field appearance="fill">
  <mat-label>Choose a date</mat-label>
  <input matInput [matDatepicker]="picker" [(ngModel)]="model" (ngModelChange)="modelChange($event)">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

From the component side, you will have to implement ControlValueAccessor, and do the transformations you need :

writeValue(value: string): void {
    this.model = transformDateFromMyFormatToIso(value);
}
modelChange(value: string) {
    const transformedValue = transformIsoDateToMyFormat(value);
    this.onChange(transformedValue);
  }

You can now add the new component to a form the way you would have added the original one.

Here is a running stackblitz example.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Date conversion to specific format using Angular pipes

分類Dev

PHP date() to Excel serial format conversion

分類Dev

Date time conversion in Javascript from format "/Date(1535515200000)/"

分類Dev

Using Moment to Validate Default Javascript Date Format

分類Dev

oracle to_date conversion showing literal does not match string format

分類Dev

unable to change date format with "/" as separator , if default system date format having "-" as date separator

分類Dev

How to format date coming from the database in angular

分類Dev

Mask Input Value Date Format Angular

分類Dev

modify date format on json array in php

分類Dev

JSON Field with date format using Javascript?

分類Dev

How to parse default git date format in C#

分類Dev

Identify date format in jquery and set it as default in a jquery datepicker

分類Dev

Angular datepicker, can't set default date without ngModel

分類Dev

Angular Bootstrap: How to Allow 2 different date format

分類Dev

Angular 6 Date format MM/dd/yyyy in reactive form

分類Dev

Angular JS Android app Unexpected token for default parameter

分類Dev

Date conversion to a different locale

分類Dev

DATE CONVERSION SCALA

分類Dev

Conversion of date formats

分類Dev

PHP String to date conversion

分類Dev

Find and filter pipe for all content of Array as JSON format Angular 2

分類Dev

Format DateTime without string conversion

分類Dev

String date format convert to date

分類Dev

Sql Server string to date conversion

分類Dev

Notice: Array to string conversion with date

分類Dev

Date conversion and subtraction in shell script

分類Dev

bigInt to date conversion giving errors

分類Dev

Excel Date& Time Conversion

分類Dev

Java String Date conversion issue

Related 関連記事

  1. 1

    Date conversion to specific format using Angular pipes

  2. 2

    PHP date() to Excel serial format conversion

  3. 3

    Date time conversion in Javascript from format "/Date(1535515200000)/"

  4. 4

    Using Moment to Validate Default Javascript Date Format

  5. 5

    oracle to_date conversion showing literal does not match string format

  6. 6

    unable to change date format with "/" as separator , if default system date format having "-" as date separator

  7. 7

    How to format date coming from the database in angular

  8. 8

    Mask Input Value Date Format Angular

  9. 9

    modify date format on json array in php

  10. 10

    JSON Field with date format using Javascript?

  11. 11

    How to parse default git date format in C#

  12. 12

    Identify date format in jquery and set it as default in a jquery datepicker

  13. 13

    Angular datepicker, can't set default date without ngModel

  14. 14

    Angular Bootstrap: How to Allow 2 different date format

  15. 15

    Angular 6 Date format MM/dd/yyyy in reactive form

  16. 16

    Angular JS Android app Unexpected token for default parameter

  17. 17

    Date conversion to a different locale

  18. 18

    DATE CONVERSION SCALA

  19. 19

    Conversion of date formats

  20. 20

    PHP String to date conversion

  21. 21

    Find and filter pipe for all content of Array as JSON format Angular 2

  22. 22

    Format DateTime without string conversion

  23. 23

    String date format convert to date

  24. 24

    Sql Server string to date conversion

  25. 25

    Notice: Array to string conversion with date

  26. 26

    Date conversion and subtraction in shell script

  27. 27

    bigInt to date conversion giving errors

  28. 28

    Excel Date& Time Conversion

  29. 29

    Java String Date conversion issue

ホットタグ

アーカイブ