Unable to validate date using moment.js

I am currently facing an issue with date validation using moment in TypeScript. Even after providing a range of different date formats and inputting dates according to these formats, the validation always returns false.

HTML

`

<p-calendar [ngModel]="author.birthDate | momentToDateString" 
[inline]="false" [required]="true"
name="inpBirthDate" #birthDate="ngModel" [showIcon]="true" 
[maxDate]="maxDate" (ngModelChange)="author.birthDate = $event" 
[dateFormat]="dateFormat"
(onInput)="author.birthDate = dateTimeUpdate($event)"            
[monthNavigator]="appConsts.calendarSettings.monthNavigator"
[yearNavigator]="appConsts.calendarSettings.yearNavigator"
[yearRange]="appConsts.calendarSettings.yearRange">
</p-calendar>

`

TS

`

dateTimeUpdate(event, isTime = false) {
    const expectedFormat = isTime ? 
    this.appConsts.expectedDateTimeFormat : 
    this.appConsts.expectedDateFormat;

    const date = event.target.value;

    if (
        event !== null && event !== undefined &&
        date !== null &&date !== undefined && date !== ''
    ) {

        let formatedDate = moment(date, expectedFormat, true);

        if (formatedDate.isValid()) {
            return formatedDate.toDate();
        }
    }

    return '';
}

`

expectedDateFormat

`

['dd-MM-YY', 'd-MM-YY']

`

expectedDateTimeFormat

`

['dd-MM-YY, h:mm:ss a', 'd-MM-YY, h:mm:ss a']

`

With the function parameter isTime set to false, the expectedDateFormat is selected. Despite giving a date like 10-03-21, it still returns false.

Answer №1

Provided the incorrect date format, but this code is functional

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

Local variables in an Angular factory are properties or variables that

Can someone help me understand why I can't seem to grasp the factory concept in Angular? Here is the factory code I'm working with: .factory('utils', ['api', '$log', '$q', function(api, $log, $q) { ...

Button for enabling and disabling functionality, Delete list in Angular 2

I am looking to toggle between the active and inactive classes on a button element. For example, in this demo, there are 5 buttons and when I click on the first button it removes the last one. How can I remove the clicked button? And how do I implement the ...

What limitations prevent me from utilizing a switch statement to refine class types in Typescript?

Unique Playground Link with Comments This is a standard illustration of type narrowing through the use of interfaces. // Defining 2 types of entities enum EntityType { ANIMAL = 'ANIMAL', PLANT = 'PLANT', } // The interface for ani ...

Separating HTML content and text from a JSON response for versatile use within various sections of an Angular 2 application using Typescript

Hello there! I am currently utilizing Angular 2 on the frontend with a WordPress REST API backend. I'm receiving a JSON response from the service, however, the WP rest API sends the content with HTML tags and images embedded. I'm having trouble s ...

React-query v5 - Updating or fetching outdated query

I am currently using the Tanstack/react-query v5.12.2 library and I am facing an issue with invalidating or refetching an inactive query using the useQueryClient() function. It seems that something has changed in version 5 of the library. I have tried sett ...

Problem with Angular: ng-show not constantly re-evaluating expression

Utilizing a variable named activeScope to manage the state and toggle between two forms. This variable updates its value when a tab is clicked, triggering changeScope. While the change in active states for the tab buttons registers correctly, the divs for ...

Employing a variable within this Angular Directive to establish validity

I'm having trouble understanding how to utilize a variable in this scenario. In my html code, I have the following snippet inside a loop: <span ng-show='myForm." + ids[i] + ".$error.missingInfo'>Wrong!</span>"; The resulting ht ...

Refreshing the display in an AngularJS directive

I've developed a custom directive for handling file uploads in my AngularJS application. The directive is used in multiple places, including on the same page more than once. Each instance of the directive is supposed to display the uploaded file name ...

Troubleshooting Problem with Angular JS Page Loading on Internet Explorer 9

My angularjs single page application is encountering issues specifically in IE9, despite functioning perfectly in Chrome and Firefox. The initial load involves downloading a substantial amount of data and managing numerous dependencies through requireJS. ...

What is the best way to transfer attributes from a nested component to its parent in Angular 10?

Within my parent component, I have a {{title}} and a {{subtitle}} that should dynamically change based on the title and subtitle of my children components. These children are rendered inside a router-outlet, with each child providing its own unique title a ...

Having an issue with ng-model not functioning correctly in md-menu with AngularJS material

I'm currently using md-menu from Angular Material and I have integrated a search box with ng-model in the list. However, I am facing an issue where I cannot access ng-model because 'md-menu-content' is out of scope. Here is my code: <div ...

How can one create a form in AngularJS using ng-model and ng-repeat to dynamically populate data from a UI model description

Check out the JSFiddle link here: http://jsfiddle.net/vorburger/hyCTA/3/. It showcases an innovative "UI modeling" concept I came up with using AngularJS. The form is not directly coded in the HTML template; instead, it's controlled by uimodel JSON wh ...

Developing a TypeScript library through modular class implementation

I have developed a Web API and now I want to streamline my code by creating a library that can be reused in any new project I create that interacts with this API. My goal is to organize my code efficiently, so I plan to have separate classes for each endp ...

Set the styling of a div element in the Angular template when the application is first initialized

I have a question about this specific div element: <div class="relative rounded overflow-clip border w-full" [style.aspect-ratio]="media.value.ratio"> <img fill priority [ngSrc]="media.value.src || ftaLogo" ...

Using the loopback framework in conjunction with famo.us: a guide

I am interested in developing a puzzle game using the famo.us framework on my website. The website itself is built using the Angular LoopBack framework. I am facing a challenge as famo.us runs on a different server than LoopBack. Any ideas on how I can ...

The Process of Developing Applications

As a newcomer to web development, I have a solid understanding of HTML and CSS, and am learning JavaScript. When considering creating a web app, would it be better for me to focus on writing the functionality in JavaScript first before integrating it int ...

Guide to setting up Angular JS integration on Liferay platform

Looking for guidance on setting up Angular JS with Liferay. Does anyone have tips on how to configure Angular JS with Liferay? ...

Is there a way to automatically close the previous accordion when scrolling to a new one on the page?

Currently, I am working with the material-ui accordion component and facing an issue where all accordions are initially opened. As I scroll down the page and reach a new accordion, I want the previous ones to automatically close. The problem arises when tr ...

Query Parameter Matching in Typescript: Retrieve and output all query parameter values that correspond to a specific parameter

Is there a way to extract all value for a specific query parameter from an URL? For example, if we have a string like ?code1=AF&code1=AE&code1=GE&code3=FW Can we search this string for all values associated with the parameter code1? So, in ...

Error 404 Spring REST Controller with AngularJS $http

I keep receiving a 404 error when trying to make a $http call from AngularJS to a Spring controller. Here is the code snippet causing the issue: Factory : factory.checkCodeAvail = function(url){ return $http({ url: url, r ...