question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add docs example of custom timezone with MomentDateAdapter

See original GitHub issue

Bug, feature request, or proposal:

[X] Feature Request

What is the expected behavior?

MomentDateAdapter should have timezone support. Moment has a timezone lib (http://momentjs.com/timezone/) which you can set the timezone manually. This is needed when you want to create a common date/datetime based in a specific timezone.

What is the current behavior?

MomentDateAdapter supports browser’s timezone only when creating a new date moment object.

What are the steps to reproduce?

  • Include MatMomentDateModule to use MomentDateAdapter
  • Create a mat-datepicker
  • Console the output (date generated)

What is the use-case or motivation for changing an existing behavior?

I do want to create a common date/datetime based in a specific timezone.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular CLI: 1.5.0 Node: 8.8.1 OS: darwin x64 Angular: 5.0.1 … animations, common, compiler, compiler-cli, core, forms … http, platform-browser, platform-browser-dynamic … platform-server, router

@angular/cdk: 5.0.0-rc0 @angular/cli: 1.5.0 @angular/flex-layout: 2.0.0-beta.10-4905443 @angular/material-moment-adapter: 5.0.0-rc0 @angular/material: 5.0.0-rc0 @angular-devkit/build-optimizer: 0.0.32 @angular-devkit/core: 0.0.20 @angular-devkit/schematics: 0.0.34 @ngtools/json-schema: 1.1.0 @ngtools/webpack: 1.8.0 @schematics/angular: 0.1.2 typescript: 2.4.2 webpack-sources: 1.0.1 webpack: 3.8.1

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
IagoBarbozacommented, Nov 16, 2017

I solved my problem extending the MomentDateAdapter, using moment-timezone.js and changing the defaultTimeZone.

With few lines you have the problem solved. I’ll create a PR for this one with something like the code bellow:

import * as moment from 'moment-timezone'; import { Moment } from 'moment-timezone';

@Injectable() export class EbDateAdapterService extends MomentDateAdapter{

private timeZone = 'America/Sao_Paulo';

constructor(@Optional() @Inject(MAT_DATE_LOCALE) dateLocale:string) { super(dateLocale); }

setTimezone(timezone: string) { moment.tz.default(timezone); }

setLocale(locale: string) { super.setLocale(locale); }

1reaction
IagoBarbozacommented, Nov 16, 2017

What do you think about line 227? It’s right next to You can create your own DateAdapter Sounds good?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set timeZone for DatePicker from Angular 2?
To set the timezone on the datepicker you need to create a custom DateAdapter as mentioned by @lee-richardson.
Read more >
Moment Timezone | Docs
To strip out unwanted data and bundle only the zone and date range data you need, add the moment-timezone-data-webpack-plugin package:
Read more >
Datepicker - Angular Material
The following example uses the range selection strategy to create a custom range picker that limits the user to five-day ranges. Date range...
Read more >
mwl-calendar-month-view - angular-calendar documentation
A custom template to use for the slide down box of events for the active day ... If using the moment date adapter...
Read more >
Datepicker form values display wrong timezone using ... - Reddit
I can't seem to get the date picker to set a date in my local time zone - it ... Datepicker form values...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found