Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the date in datepicker angular showing the last day?

I use datepicker c angular material. Here's the code:

<td [formGroup]="item">
 <div class="input-group">
  <div class="input-group-addon">
   <mat-datepicker-toggle mdSuffix [for]="picker"></mat-datepicker-toggle>
   <mat-datepicker #picker></mat-datepicker>
  </div>
  <input class="form-control" [matDatepicker]="picker" placeholder="Date" formControlName="date">
 </div>
</td>

Datapicker is in the form that sends it to the server when it is sent.

But the problem is that the values are transmitted over the last day.

For example, I chose 1/18/2018, but sent to the server 2018-01-17T22: 00: 00.000Z.

It's strange that the angular pipe for date converts the date correctly, but before displaying it, I have a request on the server for grouping by the month and the first day of the new month falls on the last day of the previous one.

Date I store in mongoose schema with type Date.

Maybe someone had a problem with this.

Thank you.

like image 510
Виталий Мудрый Avatar asked Jan 12 '18 10:01

Виталий Мудрый


People also ask

How do you use MatDatepickerModule?

After completing the installation, Import ' MatDatepickerModule,' from '@angular/material' in the app. module. ts file. Then use <mat-datepicker-toggle> tag to use angular material date picker and as well as matInput.

What is datepicker in angular?

Angular Datepicker is a built-in material component that allows us to enter the date through text input or by choosing the date from a calendar. Angular Material Datepicker allows users to enter the date through text input or by choosing the date from the calendar.

How to change the datetime format in angular?

To change the datetime format in angular we have to pass date time format parameter to the angular pipe as shown below. { { date_value | date :'short'}} // 6/15/19, 5:24 PM. The format ‘short’ is one of the predefined date formats in angular which converts our date value to ’M/d/yy, h:mm a’ format.

How does the datepicker set the time?

The datepicker sets the time to 0h0m0.000 when you choose a date. So, if you live in a GMT "negative" region, the day will be set to the day before when in UTC format. But it normally has no big side effects, unless you need to record the date as a string in the server, representing the end user's local time.

Does datepicker work with PowerApps?

07-11-2016 09:09 AM Working with Powerapps 2.0.460, Windows 10, IPad app, connection to SQL Azure (for Access Web App in Sharepoint online), local settings for UK on PC and Ipad, date format dd/mm/yyyy The date returned to the server from a DatePicker control is consistently one day less than the date entered.


Video Answer


2 Answers

I solve this problem by converting datepicker date value to UTC. Here is example:

const d = new Date(<DATE FROM FORM FIELD>);
const date = Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());

Maybe it's not elegant way to fix it but help me

like image 171
Виталий Мудрый Avatar answered Sep 30 '22 19:09

Виталий Мудрый


I had a similar problem. I solved it by configuring Datepicker to use UTC dates. (It uses local time by default) Just add some providers on your component declaration :

import {MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_LOCALE} from '@angular/material/core';

@Component({
  providers: [
    {provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]},
    {provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: {useUtc: true}}
  ]
})
like image 36
Etienne Coumont Avatar answered Sep 30 '22 19:09

Etienne Coumont