Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feature/#44-tree-sh…
Browse files Browse the repository at this point in the history
…aking
  • Loading branch information
bbortt committed Oct 30, 2019
2 parents 5ccab24 + 651b921 commit bf3b221
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
26 changes: 19 additions & 7 deletions projects/moment/src/adapter/moment-datetime-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
DateAdapter,
MAT_DATE_LOCALE
} from "@angular/material/core";
import {
MatMomentDateAdapterOptions,
MAT_MOMENT_DATE_ADAPTER_OPTIONS
} from "@angular/material-moment-adapter";
import { DatetimeAdapter } from "@mat-datetimepicker/core";

import * as moment_ from "moment";
Expand Down Expand Up @@ -36,9 +40,14 @@ export class MomentDatetimeAdapter extends DatetimeAdapter<Moment> {
narrowDaysOfWeek: string[]
};

constructor(@Optional() @Inject(MAT_DATE_LOCALE) matDateLocale: string, _delegate: DateAdapter<Moment>) {
private _useUtc = false;

constructor(@Optional() @Inject(MAT_DATE_LOCALE) matDateLocale: string,
@Optional() @Inject(MAT_MOMENT_DATE_ADAPTER_OPTIONS) matMomentAdapterOptions: MatMomentDateAdapterOptions,
_delegate: DateAdapter<Moment>) {
super(_delegate);
this.setLocale(matDateLocale || moment.locale());
this._useUtc = matMomentAdapterOptions.useUtc;
}

setLocale(locale: string) {
Expand Down Expand Up @@ -91,7 +100,10 @@ export class MomentDatetimeAdapter extends DatetimeAdapter<Moment> {
}

// const result = moment({year, month, date, hour, minute}).locale(this.locale);
const result = moment({year, month, date, hour, minute});
let result = moment({ year, month, date, hour, minute });
if (this._useUtc) {
result = result.utc();
}

// If the result isn't valid, the date must have been out of bounds for this month.
if (!result.isValid()) {
Expand All @@ -102,7 +114,7 @@ export class MomentDatetimeAdapter extends DatetimeAdapter<Moment> {
}

private getDateInNextMonth(date: Moment) {
return super.clone(date).date(1).add({month: 1});
return super.clone(date).date(1).add({ month: 1 });
}

getFirstDateOfMonth(date: Moment): Moment {
Expand All @@ -118,14 +130,14 @@ export class MomentDatetimeAdapter extends DatetimeAdapter<Moment> {
}

addCalendarHours(date: Moment, hours: number): Moment {
return super.clone(date).add({hours});
return super.clone(date).add({ hours });
}

addCalendarMinutes(date: Moment, minutes: number): Moment {
return super.clone(date).add({minutes});
return super.clone(date).add({ minutes });
}

deserialize(value: any): Moment | null {
return this._delegate.deserialize(value);
}
return this._delegate.deserialize(value);
}
}
10 changes: 8 additions & 2 deletions src/app/moment/moment.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Validators
} from "@angular/forms";
import { DateAdapter } from "@angular/material/core";
import { MomentDateAdapter } from "@angular/material-moment-adapter";
import { MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS } from "@angular/material-moment-adapter";
import {
DatetimeAdapter,
MAT_DATETIME_FORMATS,
Expand All @@ -29,6 +29,12 @@ import {
provide: DatetimeAdapter,
useClass: MomentDatetimeAdapter
},
{
provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS,
useValue: {
useUtc: true
}
},
{
provide: MAT_DATETIME_FORMATS,
useValue: {
Expand All @@ -41,7 +47,7 @@ import {
display: {
dateInput: "L",
monthInput: "MMMM",
datetimeInput: "L LT",
datetimeInput: "L LT ZZ",
timeInput: "LT",
monthYearLabel: "MMM YYYY",
dateA11yLabel: "LL",
Expand Down

0 comments on commit bf3b221

Please sign in to comment.