String date in the format "dd-MM-yyyy" cannot be transformed into a date using the 'DatePipe' function

Having trouble with date conversion in my custom pipe. It seems that when using a locale of 'nl-nl' and trying to format the date as 'dd-MM-YYYY', I'm seeing an error message stating

Unable to convert "16-02-2023" into a date for pipe 'DatePipe'
, while other dates like 02-09-2023 work fine.

import { DatePipe } from '@angular/common';
import { Pipe, PipeTransform } from '@angular/core';
import { ConfigurationService } from '../services/configuration.service';

@Pipe({
    name: 'formattedDate',
})
export class FormattedDatePipe implements PipeTransform {
    constructor(
        private datePipe: DatePipe,
        private configurationService: ConfigurationService,
    ) {}

    transform(value: Date | string | number): string {
        return this.datePipe.transform(value, this.configurationService.config.customer.dateFormat);
    }
}

I suspect there may be confusion between day and month values causing the issue, but I haven't been able to pinpoint the exact cause.

Answer №1

import { DatePipe } from '@angular/common';
import { Pipe, PipeTransform } from '@angular/core';
import { ConfigurationService } from '../services/configuration.service';

@Pipe({
    name: 'formattedDate',
})
export class FormattedDatePipe implements PipeTransform {
    constructor(
        private datePipe: DatePipe,
        private configurationService: ConfigurationService,
    ) {}

    transform(value: Date | string | number): string {
       
        return this.datePipe.transform(value, 'dd-MM-yyyy', 'nl-nl');
    }
}

In order to rectify the issue at hand, it is crucial to ensure that the date format being utilized corresponds with the norms of the 'nl-nl' locale. Within the Netherlands, the customary date structure typically follows 'dd-MM-yyyy,' wherein the year is denoted in lowercase as 'yyyy,' rather than uppercase as 'YYYY.' By conforming to these guidelines, potential errors stemming from date formatting within a Dutch context can be mitigated effectively.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it necessary to unsubscribe from an Angular HTTP-Request when using switchMap?

I have a question about managing subscriptions in my Angular application. I'm currently using the combineLatest operator to create an observable from two Angular observables, route.params and route.queryParams. This combined observable is then used as ...

What is the best way to link assets within an Angular custom element (Web Components)?

After successfully creating a web component and referencing an image from my asset folder, everything was running smoothly on my local environment. However, when I published my custom element to Firebase hosting, I encountered some issues. When trying to ...

What are the steps to ensure that the submit button remains disabled until all form data is filled out with reactive validation?

Is there a way to prevent a form button from being enabled until all required data is entered in a reactive form? You can find the code for my reactive form here. Additionally, how can I reset a reactive form and clear all data after it has been submitted ...

Bypass VueJs Typescript errors within the template section with Typescript

My VueJs App is functioning properly, but there is one thing that bothers me - a TypeScript error in my template block. Is there a way to handle this similar to how I would in my script block? <script setup lang="ts"> //@ignore-ts this li ...

The SpinButton object has an undefined public property called 'value' and the public method 'focus()' is not available

Recently, I've delved into using reactjs, typescript, and Office UI Fabric. However, I'm facing a challenge with one of the components from fabric. In this scenario, I have a simple Product component containing a SpinButton and a DefaultButton. M ...

Encountering issues with MatToolbar in Angular 9 causes unexpected errors

Running Angular version 9.2.0 Encountering an issue while importing the MatToolbarModule in a module and utilizing it in the HTML template. The error message reads as follows: This could indicate that the library (@angular/material/toolbar) that declar ...

How can I perform email validation using Angular 6?

I am working on an Angular6 form that includes a field for email input. Currently, the email field has proper validation which displays an error message when an invalid email is entered. However, even if the error message is shown, the form is still saved ...

What is the best way to include a new property to an existing interface and then export the updated interface in Typescript?

Can you provide guidance on creating a new interface - UIInterface that combines SummaryInterface with additional properties? For example: import { SummaryInterface } from 'x-api'; // summaryInterface includes 20+ predefined properties generated ...

Unable to connect a unique FormGroup (using ControlValueAccessor) within a FormArray

We are working with two components, referred to as parent and child, both implementing ControlValueAccessor. The parent form is defined as follows: this.formBuilder.group({ children: this.formBuilder.array([]) }) While the child form looks like this: ...

Should we rethink using nested *ngFor loops in Angular 7?

Currently, I am dealing with an object that consists of an array. This array has the capability to contain one or multiple objects similar to the parent object. The levels of nesting in this structure could potentially be infinite. To showcase all the data ...

Angular Reactive Forms - Adding Values Dynamically

I have encountered an issue while working with a reactive form. I am able to append text or files from the form in order to make an http post request successfully. However, I am unsure about how to properly append values like dates, booleans, or arrays. a ...

Utilizing Vue and Typescript for efficient dependency injection

After attempting to use vue-injector, I encountered an issue as it was not compatible with my version of Vue (2.6.10) and Typescript (3.4.5). Exploring other alternatives, there seem to be limited options available. Within the realm of pure typescript, t ...

The parameter type 'void' cannot be assigned to the parameter type 'SetStateAction<{}>'

Currently, I am in the process of developing an application utilizing TMDB with NextJS and Typescript. Within my movies.ts file, I have implemented the following code: export async function getTrendMovies() { const url = 'https://api.themoviedb.o ...

Transferring an array of interfaces between Angular 2 components

Encountering issues with passing an Array of data between components using @Input. Here is the code snippet: public ngOnInit() { this.applications = new Array<ApplicationEntryInterface>(); (...) let shortTermData = new ShortTermApplicationAdapte ...

Validating forms using TypeScript in a Vue.js application with the Vuetify

Currently, I am attempting to utilize Vue.js in conjunction with TypeScript. My goal is to create a basic form with some validation but I keep encountering errors within Visual Studio Code. The initial errors stem from my validate function: validate(): v ...

Transfer only designated attributes to object (TS/JS)

Is it feasible to create a custom copy function similar to Object.assign(...) that will only copy specific properties to the target? The code snippet I have is as follows: class A { foo?: string; constructor(p: any) { Object.assign(this, p ...

I am curious if there is a method to obtain the component's logic or the value of an element's style within animation metadata in Angular

Is there a way to determine the height of a dynamically created component from a recursive tree without using the tree inside metadata declarations? One possibility is calculating the height inside the *ngStyle directive as shown below: <my-directory [ ...

Utilizing an AwsCustomResource in AWS CDK to access JSON values from a parameter store

I found a solution on Stack Overflow to access configurations stored in an AWS parameter. The implementation involves using the code snippet below: export class SSMParameterReader extends AwsCustomResource { constructor(scope: Construct, name: string, pr ...

Tips for extracting a keyword or parameters from a URL

I'm in the process of creating my personal website and I am interested in extracting keywords or parameters from the URL. As an illustration, if I were to search for "Nike" on my website, the URL would transform into http://localhost:3000/searched/Nik ...

Learning how to merge two observable streams in Angular2 by utilizing RxJS and the powerful .flatMap method

Within my Angular2 app, I have created an Observable in a Service called ContentService. Here is a simplified version of the code: @Injectable() export class ContentService { constructor(private http:Http, private apiService:ApiService) { th ...