2

I'm working in angular project. In there users will only able to create task for today or upcoming dates using date picker.

I'm using <mat-datepicker> with moment to disable the previous dates.

<mat-form-field formGroupName="user">
        <mat-label>Due Date</mat-label>
        <input
          matInput
          [min]="dueDateMin"
          [matDatepicker]="picker"
          (click)="picker.open()"
          placeholder="Choose a date"
          formControlName="date"
          (dateChange)="setTaskDueDate($event.value); checkAvailableEfforts()"
          required
        />
        <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
        <mat-hint>Task completion date.</mat-hint>
        <mat-error>Due date is required</mat-error>
        <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

In Component

dueDateMin = moment();

Problem: User can enable the fields by change the timezone of his browser.

Question: Is there any option by which I can showing an alert on app if user will open the app in another timezone instead of mentioned timezone. for eg: "Asia/Calcutta" India Timezone.

or Use the same timezone regardless the user's machine timezone.

Thanks in advance!

Neeraj Kumar
  • 436
  • 1
  • 4
  • 13
  • [This answer may guide you in the right direction](https://stackoverflow.com/questions/439630/create-a-date-with-a-set-timezone-without-using-a-string-representation/439871#439871) – Shobhit Nov 26 '19 at 15:38
  • [This answer may guide you in the right direction](https://stackoverflow.com/questions/439630/create-a-date-with-a-set-timezone-without-using-a-string-representation/439871#439871) – Shobhit Nov 26 '19 at 15:39
  • Hi Shobhit, UTC offset will only fix the date from specific timezone but as per problem we don't know what timezone user will set at their system to re-enable the dates in datepicker . – Neeraj Kumar Nov 26 '19 at 15:43

2 Answers2

0

To have a fixed time, try setting your default time to the zone you need.

moment.tz.setDefault(your_timezone);

For example:

moment.tz.setDefault("America/New_York");
Angela Amarapala
  • 1,002
  • 10
  • 26
0

You should avoid setting a timestamp from your front end code because the user's clock might not be synchronised properly. If you need to set a timestamp on a document, use the firestore server timestamp that will ensure that all users are sharing the same clock. Use serverTimeStamp for consistent times. Refer code snippet below -

const { serverTimestamp, increment } = firebase.firestore.FieldValue; ref.update({ timestamp: serverTimestamp() })

Hope this helps!

Shobhit
  • 93
  • 7