1

I have two inputs in my application and i'm trying to combine them in a single object (cuz the server expect a single object)

template:

<ion-item>
            
            <mat-form-field>
                <mat-label>Data Appuntamento</mat-label>
                <input matInput [matDatepicker]="picker" [(ngModel)]="this.DataPrenotazione" >
                <mat-hint>DD/MM/YYYY</mat-hint>
                <mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
                <mat-datepicker #picker></mat-datepicker>
            </mat-form-field>
            <ion-label>Ora Appuntamento</ion-label>
            <ngx-timepicker-field [format]="24" [(ngModel)]="this.OraPrenotazione" [minutesGap]="15"></ngx-timepicker-field>
            
        </ion-item>

i then call this to combine the inputs:

combineDateTime(date: Date, time: Date): Date {
        const combinedDateTime = new Date();
        combinedDateTime.setFullYear(date.getFullYear());
        combinedDateTime.setMonth(date.getMonth());
        combinedDateTime.setDate(date.getDate());
        combinedDateTime.setHours(time.getHours());
        combinedDateTime.setMinutes(time.getMinutes());
        return combinedDateTime;
    }

when i try to call the combine function i get the ".getFullYear() is not a function"

this.combineDateTime(this.DataPrenotazione, this.OraPrenotazione)

1
  • what u receive in date? console.log(date,time) and watch... You can receive moment, not Date (if plugin installed) or something else Commented May 7, 2024 at 10:56

1 Answer 1

1

We are getting the output from the timepicker as 02:30 so we need to split it accordingly. We can use string split on :, then use the unary + to convert the split string to number and finally set the hours and minutes!

  combineDateTime(date: Date, time: string): Date {
    const combinedDateTime = new Date();
    combinedDateTime.setFullYear(date.getFullYear());
    combinedDateTime.setMonth(date.getMonth());
    combinedDateTime.setDate(date.getDate());
    const [hours, minutes] = time.includes(':') ? time.split(':') : [0, 0];
    combinedDateTime.setHours(+hours);
    combinedDateTime.setMinutes(+minutes);
    return combinedDateTime;
  }

FULL CODE:

TS:

import { Component } from '@angular/core';
import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';
import { provideNativeDateAdapter } from '@angular/material/core';
import { FormsModule } from '@angular/forms';
import { NgxMaterialTimepickerModule } from 'ngx-material-timepicker';
import { CommonModule } from '@angular/common';

/** @title Basic datepicker */
@Component({
  selector: 'datepicker-overview-example',
  templateUrl: 'datepicker-overview-example.html',
  standalone: true,
  providers: [provideNativeDateAdapter()],
  imports: [
    MatFormFieldModule,
    MatInputModule,
    MatDatepickerModule,
    FormsModule,
    NgxMaterialTimepickerModule,
    CommonModule,
  ],
})
export class DatepickerOverviewExample {
  OraPrenotazione: string = '00:00';
  DataPrenotazione: Date = new Date();
  combinedDateTime: any = null;

  combineDateTime(date: Date, time: string): Date {
    const combinedDateTime = new Date();
    combinedDateTime.setFullYear(date.getFullYear());
    combinedDateTime.setMonth(date.getMonth());
    combinedDateTime.setDate(date.getDate());
    const [hours, minutes] = time.includes(':') ? time.split(':') : [0, 0];
    combinedDateTime.setHours(+hours);
    combinedDateTime.setMinutes(+minutes);
    return combinedDateTime;
  }
}

HTML:

<mat-form-field>
  <mat-label>Data Appuntamento</mat-label>
  <input
    matInput
    [matDatepicker]="picker"
    [(ngModel)]="this.DataPrenotazione"
  />
  <mat-hint>DD/MM/YYYY</mat-hint>
  <mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>
<label>Ora Appuntamento</label>
<ngx-timepicker-field
  [format]="24"
  [(ngModel)]="this.OraPrenotazione"
  [minutesGap]="15"
></ngx-timepicker-field>

<button
  (click)="combinedDateTime = combineDateTime(DataPrenotazione, OraPrenotazione)"
>
  combine
</button>

Final DATE: {{combinedDateTime | date : 'full'}}

Stackblitz Demo

Sign up to request clarification or add additional context in comments.

2 Comments

this works! is it possibile to have it DD/MM/YYYY? does it depend by provideNativeDateAdapter()?
@FrancescoTambellini Try referring this angular material example code [here]material.angular.io/components/datepicker/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.