Encountering an issue with PrimeNG's <p-calendar> component: the error message "date

I encountered an issue resulting in the following error message:

core.es5.js:1020 ERROR Error: Uncaught (in promise): TypeError: date.getMonth is not a function

TypeError: date.getMonth is not a function

This error occurs whenever I attempt to implement two-way data binding with my JSON object received from the backend. Although the data type on the backend is Date, it does not display properly in the p-calendar input field. Seeking guidance for resolution.

{ "applicationId": 1, "entryDate": 1524665731000, "ongoingDescription": "<p>Ongoing entry should end in (5081421)</p>", "activeFlag": "Y" }

HTML

<div class="form-group">
  <label>Date of the Ongoing Incident</label>
  <br>
  <p-calendar required [(ngModel)]="entry.entryDate" name="entryDate" #entryDate="ngModel" [showIcon]="true" [showTime]="true" dateFormat="mm/dd/y 'EST'" hourFormat="24"></p-calendar>
  <div class="alert alert-danger" *ngIf="entryDate.touched && !entryDate.valid" >The date and time of the incident are required</div>
</div>

TS

 entryDate: Date;

 entry = {
 }

constructor(private service: OngoingService, private route: ActivatedRoute, 
private router: Router, private location: Location) {

this.id = this.route.snapshot.paramMap.get('id');
if (this.id) this.service.get(this.id).take(1).subscribe(service => 
this.entry = service);

}

ngOnInit() {

}

ngAfterContentInit() {

}

ngAfterViewInit() {

}

Your insights and advice are greatly appreciated.

I have attempted solutions like initializing entryDate = new Date(); and entryDate = new Date(this.entryDate);

Answer №1

In order to convert to Date from JSON, manual assignment is required as it does not happen automatically.

this.id = this.route.snapshot.paramMap.get('id');
if (this.id) {
    this.service.get(this.id).take(1)
        .subscribe(service => this.entry = { entryDate: new Date(service.entryDate) });
}

The variable entryDate remains unassigned and unused in your code. Unless you specifically need it for referencing the p-calendar component's properties and methods, you can safely remove it. If needed, make sure to include an attribute decorator like:

@ViewChild('entryDate')
entryDate: Calendar;

Keep in mind that the type should be Calendar, not Date. This is a primeng type and must be imported from primeng library

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

Unable to transform the singular JSON object received from the server into the necessary format in order to analyze the data

Forgive me if my questions seem simple, as I am new to working with Angular. I'm having trouble understanding how to handle a JSON object received from the server and convert it into a custom datatype to use for rendering HTML using ngFor. I've ...

Using TypeScript to define generic types for classes, method parameters, and method return types

I am facing an issue with this particular function function getCollection<T>(collectionType: T): Collection<T> { return new Collection<T>() } In the Collection class, I have the following code snippet export class Collection<T> { ...

Angular CORS Policy

I encountered an issue when trying to send an authorization header JWT token from Angular. The error message reads: Access to XMLHttpRequest at 'http://localhost:52278/api/user/signup' from origin 'http://localhost:4200' has been blocke ...

Switch up row values in an array and transform them into an object using SheetJS

I am struggling to format an array where each "Working Day" is represented as an object with specific details like index and start/end date. I need help manipulating the JSON data to achieve the desired structure. The package I'm currently using is: ...

An iron-dropdown made of polymer featuring a trigger button made of paper-icon-button

I am facing an issue on my Angular site where I have integrated Polymer. I am trying to add a notifications section to the header, but I am struggling to make the button functional. How can I bind the click event of the button to trigger the dropdown? Thi ...

Can you explain the purpose of the MomentInput type in ReactJS when using TypeScript?

I am currently facing an issue where I need to distinguish between a moment date input (material-ui-pickers) and a normal text input for an event in my project. const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => { const i ...

Issues encountered with the `npm install` command in Azure build pipelines for a simple Angular application

Transferred the GitHub Repository mentioned in this link to Azure Repository. Established the Build Pipeline using Classic Editor, and you can find the YAML Code below: trigger: branches: include: - refs/heads/master jobs: - job: Job_1 display ...

Differences between Angular components and TypeScript classes in models

In my observation, I have noticed that in several instances, manual models are being created for components to specifically manage the data. Despite this, the component already contains a ts class along with the html and css data. Shouldn't the task ...

When Angular 8 is accessed over USB, the net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK) error occurs while trying to retrieve the styles

When running my Angular 8 project locally using ng serve, the bundle size is approximately 7 MB and loads smoothly on the desktop where it's hosted. However, when attempting to access the website from a phone connected via USB with port forwarding fo ...

Is there a method to ensure the strong typing of sagas for dispatching actions?

Within redux-thunk, we have the ability to specify the type of actions that can be dispatched enum MoviesTypes { ADD_MOVIES = 'ADD_MOVIES', } interface AddMoviesAction { type: typeof MoviesTypes.ADD_MOVIES; movies: MovieShowcase[]; } typ ...

Are you ready to dive into the world of running an ASP.NET MVC project with Angular in Visual Studio

Currently, I am working on developing a CRUD application using ASP.NET MVC in Visual Studio with Angular. I am interested in running the entire project solely through VS Code without relying on Visual Studio. Does anyone have a solution for achieving thi ...

Navigate through collections of objects containing sub-collections of more objects

The backend is sending an object that contains an array of objects, which in turn contain more arrays of objects, creating a tree structure. I need a way to navigate between these objects by following the array and then back again. What would be the most ...

Having trouble with the Angular Material component? The element 'mat-option' is not recognized

I am having trouble with implementing an Angular Material component. The component is not functioning properly, and I received the following error message: Uncaught Error: Template parse errors: 'mat-option' is not a known element: // ... I sus ...

Show a dropdown menu based on a certain condition in Angular

Is there a way to conditionally display select options like this? <select id="updateType" class="form-control" formControlName="updateType"> <option value="personalDetails">Personal</option> <option value="addressD ...

Unable to access file: EACCES: permission denied when trying to open '/Users/emilio/.ionic/daemon.log' - Error occurred due to lack of permissions

Hello everyone in the Ionic community! I've been working on an Ionic v3 project and everything was running smoothly. However, when I attempted to integrate the Geolocation module from @ionic-native/geolocation, I encountered an error message stating: ...

Angular - ngbDropdownMenu items are hidden from view, but their presence is still detectable

Hey there! I'm currently working on a school project and I'm aiming to implement a drop-down menu. Surprisingly, it's proving to be more challenging than expected. <div class="parrent"> <div class="row"> ...

Angular4 + Universal + ng-bootstrap triggering an 'Unexpected token import' error

I recently made the leap to upgrade my angular version from 2+ to 4+ in order to take advantage of the universal package for server-side rendering, specifically for SEO purposes. Following the necessary steps and configurations outlined at https://github.c ...

Iterating over a JSON array using *ngFor

Here is the JSON structure that I have: { "first_name": "Peter", "surname": "Parker", "adresses": { "adress": [{ "info1": "intern", "info2": "bla1" }, { "info1": "extern", "info2": "bla2" }, { "info1": " ...

Show a nested JSON object when the specific key is not recognized

I received the following data from my API: { "id": 82, "shortname": "testing2", "fullname": "test2", "address": "addrtest2", "telephone" ...

What's the best way to declare a component that is intended to be used across all pages of my application?

I have a HeroLogoComponent that is included in the header of every page in my app. It is declared in the app.module like this: @NgModule({ declarations: [ MyApp, HeroLogoComponent, ], ... Based on what I have read in the Angular documentation, usin ...