Tips for retrieving a date and time selection from a mat-date-picker and mat select?

I am currently utilizing Angular calendar to display various events. Each event is defined by the following parameters:

event:{
    title: string,
    start: Date,
    end: Date
};

As material design does not offer a date-time picker, I have opted for using a separate datepicker to choose the date and then a mat select for selecting the time within an 8AM-6PM range. The setup looks similar to this:

  <div class = "date-picker">

    <mat-form-field appearance="fill">
      <mat-label>Select day</mat-label>
      <input matInput [matDatepicker]="picker" [(ngModel)]="date" name = "date">
      <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
      <mat-datepicker #picker></mat-datepicker>
    </mat-form-field>
  </div>

  <div class ="time-picker">
    <mat-form-field appearance="fill">
      <mat-label>Time</mat-label>
      <mat-select [(ngModel)]="time" name = "time">
        <mat-option *ngFor="let time of times" [value]="time">{{time}}</mat-option>
      </mat-select>
    </mat-form-field>
  </div>
</div>

I want to combine the selected "date" and "time" values into one complete Date object, how can I achieve that?

The expected date format would be:

new Date(2021, 6, 1, 12, 30, 0, 0)

In my .ts file, I simply do:

this.event = {
      start: new Date(this.date.getFullYear(), this.date.getUTCMonth(), this.date.getUTCDay(), this.time, 0, 0, 0),

etc...}

I need the "start" parameter to represent a proper Date format combining the "date" and "time" values.

Answer №1

getDay returns the day of the week, but you should use getDate instead. Avoid using UTC versions if you want to work with your local timezone (which is necessary for a calendar).

Consider trying out this expression:

new Date(this.date.getFullYear(), this.date.getMonth(), this.date.getDate(), this.time)

This code assumes that your times range from 0 to 23.

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

I am looking to test my Angular 2 application using Team City - what testing solutions are available to me?

In the upcoming two-year project, we are embarking on the development of a robust Angular 2 application. As part of our testing strategy, we will be implementing User Interface Tests. While our Unit Tests and Integration Tests will be written in C# using ...

Trouble with running the "npm run tsc" command in Angular 2 beta

I'm facing an issue with compiling my Typescript using the command npm run ts. What's odd is that I can successfully compile and run it by running npm start. Below is the log: 0 info it worked if it ends with ok 1 verbose cli [ 'node' ...

The user authentication service indicates that no user is currently logged in

Currently, I am working on implementing a route guard to distinguish between authenticated users and guests. In order to achieve this, I have created an auth-guard service along with an auth service. Although the user data is successfully stored in the loc ...

Retrieving an array using Mongoose and Typegoose by specifying the start and end index

Is there a way to retrieve a specific array slice directly from MongoDB? I am attempting to achieve something similar to this: Model.find({filter: option}, startindex, endindex. Currently, the only method I have discovered is as follows: let result = await ...

Tips for retrieving a cropped image with Croppr.js

Currently, I am developing a mobile application using Ionic 3. Within the application, I have integrated the Croppr.js library to enable image cropping before uploading it to the server. However, I am facing an issue where I am unable to retrieve the cropp ...

I'm unable to provide an ID value within the modal section

<div class="table"> .... <tr *ngFor="let product of products; index as i"> <th scope="row">{{ i + 1 }}</th> <td>{{ product.name }}</td> <td>{{ product.category }}</td> <td> ...

Using ESLint to enforce snake_case naming conventions within TypeScript Type properties

When working with TypeScript, I prefer to use snake_case for properties within my Interfaces or Types. To enforce this rule, I have configured the ESLint rule camelcase as follows: 'camelcase': ["error", {properties: "never"}], Even though the E ...

Is TypeScript React.SFC encountering incompatibility issues with types?

Trying to figure out TypeScript but struggling to get rid of these persistent errors. I've tried multiple approaches and resorted to using any wherever possible, but the errors still persist: (9,7): Type '(props: IRendererProps & { children? ...

I am looking to invoke the Token API from Microsoft Graph during an Angular 7+ HTTP request

My goal is to make an API call from my Angular application to retrieve an access token from . With this token, I then aim to access the https://graph.microsoft.com/v1.0/users/##UserId##​​​​​​​​​​​​​/getMemberGroups endpoint withou ...

Angular HTML layout designed for seamless interaction

<div class ="row"> <div class ="col-md-6"> Xyz </div> <div class ="col-md-6"> Abc </div> </div> Using the code above, I can easily create a layout with two columns. Th ...

Passing a variable to a cloned template in Angular 2: A guide

When working with Angular2, I encountered an issue with my template code that I am cloning every time a user clicks a button. Despite following instructions provided in this post How to dynamically add a cloned node in angular2 (equivalent to cloneNode), I ...

Angular 2 - mistakenly spelled component name in selector tag leading to error notifications

As a newcomer to Angular2, I am uncertain about the most suitable term to use when referring to a selector/component tag. An instance of what I'm calling a selector/component tag is the app-menu tag showcased in the HTML sample below. In case any miss ...

Currently, there is a requirement to include past build outcomes in the HTML test report within the playwright

Is there a way to display the previous build status of each test case for every test case? I have been attempting to use test.info() in playwright, but it seems inaccessible from onTestEnd. One option could be to retrieve the previous build data from Jenki ...

An issue (TC2322) has been encountered during the compilation of the Angular4 application

Encountered an issue while running the "ng build" command: ERROR in src/app/model/rest.datasource.ts(34,5): error TS2322: Type 'Observable<Product | Order | Product[] | Order[]>' is not assignable to type 'Observable<Product[]>& ...

Should one bother utilizing Promise.all within the context of a TypeORM transaction?

Using TypeORM to perform two operations in a single transaction with no specified order. Will utilizing Promise.all result in faster processing, or do the commands wait internally regardless? Is there any discernible difference in efficiency between the t ...

Sinon - observing a spy that remains inactive, yet the test proceeds to enter the function

Having some trouble with a test function that uses two stubs. The stubs seem to be working fine, as I can see the spy objects inside when I console.log res.json or next. However, the spy is not being called when I make the assertion. The error message read ...

Reasons Why Optional Chaining is Not Utilized in Transpiling a Node.js + TypeScript Application with Babel

Currently, I am delving into Babel in order to gain a deeper understanding of its functionality. To facilitate this process, I have developed a basic API using Node.js and TypeScript. Upon transpiling the code and initiating the server, everything operates ...

The Select element in angular fails to choose the intended option when tested with Cypress

I am encountering a challenge in my automation testing project where I need to select an option from a select element. The project is built using Angular and I am using Cypress for automation testing. As a newcomer to Cypress, I followed the instructions ...

Choose a Spot on the Open Layers map using a marker or icon

As a beginner in the world of Open Layers, I'm eager to learn how to utilize markers or icons to obtain user location. Additionally, I hope to harness the power of Angular to extract these location details. ...

Choose FormGroup using Angular Directive

Can Angular reactive form controls be selected for a custom directive in a different way? Take a look at this code snippet: @Directive({ selector: '[formControl], [formControlName]', }) export class MyDirective { constructor( priv ...