I have a Material Datepicker from which I would like to only get date with no timestamp. Since my timezone is GMT+2, API always receives something like 03.04.2018 22:00:00 UTC, but I would like to save the dates in local.
My datepicker: "Wed Apr 11 2018 00:00:00 GMT+0200 (Central Europe Daylight Time)"
<div class="input-group">
<input matInput [matDatepicker]="start" readonly="readonly" class="form-control" formControlName="start" name="start" >
<div class="date__picker__button">
<mat-datepicker-toggle matSuffix [for]="start"></mat-datepicker-toggle>
<mat-datepicker #start [disabled]="!formulationForm.enabled"></mat-datepicker>
</div>
</div>
It is possible to format the datepicker value before calling API but I was hoping there is a better solution.
Connecting a date-picker to an inputThe date-picker is made up of text input and a calendar pop-up, which is linked via the mat-Date-picker property to the text input. It also has an optional date-picker toggle button that gives the user a simple method to open the date-picker pop-up window.
To disable only datepicker toggle we can add dispabled property to the mat-datepicker-toggle element. In this case user can only enter date by manually typing.
Date pickers look like text boxes, except that a small calendar icon appears on the right side of the box. To open the pop-up calendar, users click the calendar icon. When the calendar appears, users can click the date that they want on the calendar or use the right and left arrow buttons to scroll through the months.
Yes, there are 2 ways that I know works.
// date.helpers.ts
import { formatDate } from '@angular/common';
import { Injectable } from '@angular/core';
import { NativeDateAdapter } from '@angular/material/core';
export const PICK_FORMATS = {
parse: { dateInput: { month: 'short', year: 'numeric', day: 'numeric' } },
display: {
dateInput: 'input',
monthYearLabel: { year: 'numeric', month: 'short' },
dateA11yLabel: { year: 'numeric', month: 'long', day: 'numeric' },
monthYearA11yLabel: { year: 'numeric', month: 'long' }
}
};
@Injectable({
providedIn: 'root'
})
export class PickDateAdapter extends NativeDateAdapter {
format(date: Date, displayFormat: Object): string {
if (displayFormat === 'input') {
return formatDate(date, 'dd/MM/yyyy', this.locale);;
} else {
return date.toDateString();
}
}
}
// add this to your.module.ts
providers: [
NomineeService,
{ provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: { appearance: 'legacy' } },
{ provide: DateAdapter, useClass: PickDateAdapter },
{ provide: MAT_DATE_FORMATS, useValue: PICK_FORMATS },
]
// shared.service.ts
formatDate(date): string {
const _date = new Date(date);
const day = _date.getDate();
const month = _date.getMonth() + 1;
const year = _date.getFullYear();
return `${year}-${month}-${day}`;
}
formatTime(date: Date): string {
const _date = new Date(date);
const hours = _date.getHours()
const minutes = _date.getMinutes();
const seconds = _date.getSeconds();
return `${hours}:${minutes}:${seconds}`;
}
toDateTimestamp(date: Date): string {
const dateStamp = this.formatDate(date);
const timeStamp = this.formatTime(date);
return `${dateStamp} ${timeStamp}`
}
calculateDays(fromDate, toDate): number {
const FromDate = new Date(fromDate);
const ToDate = new Date(toDate);
const difference = ToDate.getTime() - FromDate.getTime();
const days = Math.round((difference / (1000 * 60 * 60 * 24)));
return days;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With