Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 - material 2 - md datepicker set first day of the week

How to set first day of the week using material 2?

Currently the default is sunday even if I set the locale to hu or anything else..

like image 692
Béla Olcsán Avatar asked Jul 12 '17 12:07

Béla Olcsán


5 Answers

Found a workaround:

Write this class

import {NativeDateAdapter} from '@angular/material';
import {Injectable} from '@angular/core';

@Injectable()
export class MyDateAdapter extends NativeDateAdapter {

  getFirstDayOfWeek(): number {
    return 1;
  }

}

Then import to the app.module like:

{provide: DateAdapter, useClass: MyDateAdapter},
like image 160
Béla Olcsán Avatar answered Oct 03 '22 17:10

Béla Olcsán


constructor(
    private dateAdapter: DateAdapter<Date>
  ) { }

  ngOnInit() {
    this.dateAdapter.setLocale('es');
    this.dateAdapter.getFirstDayOfWeek = () => { return 1; }
  }
like image 24
David Pestana Avatar answered Oct 03 '22 17:10

David Pestana


Pretty much the sam answer as Béla but I had to implement the constructor to get it working.

import { NativeDateAdapter } from '@angular/material';
import { Injectable } from '@angular/core';
import { Platform } from '@angular/cdk/platform';

@Injectable()
export class SwedishDateAdapter extends NativeDateAdapter {
  constructor() {
    super('sv-SE', new Platform());
  }

  getFirstDayOfWeek(): number {
    return 1;
  }
}
like image 22
Magnus Erkenfelt Avatar answered Oct 01 '22 17:10

Magnus Erkenfelt


I'm currently using angular 6 with the corresponding version of the material.

The solutions above worked however since i was using the moment.js adapter the application stopped working correctly.

Using the code at https://github.com/angular/material2/blob/master/src/material-moment-adapter/adapter/moment-date-adapter.ts and injecting it into the module made the application detect the correct region and set the starting day to monday (mine was pt-pt).

The only change needed was to set the correct moment import.

like image 23
GatoPingado Avatar answered Sep 30 '22 17:09

GatoPingado


Simply add the following to you app.module.ts:

  providers: [
    { provide: LOCALE_ID, useValue: 'en-GB' }
  ],
like image 27
user7518s Avatar answered Oct 04 '22 17:10

user7518s