DEV Community

Cover image for Ionic 6.1: Disabling Specific Dates, Weekends and Holidays
Sean Perkins
Sean Perkins

Posted on

Ionic 6.1: Disabling Specific Dates, Weekends and Holidays

Ionic Framework 6.1 introduces the ability to control the disabled status of each individual day within an ion-datetime.

With the isDateEnabled function, you can write custom rules to disable specific dates, weekends, months, holidays, etc.

isDateEnabled accepts a ISO 8601 date string and must return a boolean (true for enabled, false for disabled).

Here are a few common scenarios you may run into with your applications:

Disabling Specific Dates

Disables January 1, 2022.

Datetime with January 1, 2022 disabled

<ion-datetime></ion-datetime>
Enter fullscreen mode Exit fullscreen mode
import { getDate, getMonth, getYear } from 'date-fns';

const isDateEnabled = (dateIsoString: string) => {
  const date = new Date(dateIsoString);
  if (getDate(date) === 1 && getMonth(date) === 0 && getYear(date) === 2022) {
    return false;
  }
  return true;
};

const datetime = document.querySelector('ion-datetime');
datetime.isDateEnabled = isDateEnabled;
Enter fullscreen mode Exit fullscreen mode

Disabling Weekends

Disables Saturday and Sunday.

Datetime with weekends disabled

<ion-datetime></ion-datetime>
Enter fullscreen mode Exit fullscreen mode
import { isWeekend } from 'date-fns';

const isDateEnabled = (dateIsoString: string) => {
  const date = new Date(dateIsoString);
  return !isWeekend(date);
};

const datetime = document.querySelector('ion-datetime');
datetime.isDateEnabled = isDateEnabled;
Enter fullscreen mode Exit fullscreen mode

Disabling Holidays

Similar to disabling specific dates, you can also disable specific dates and months without comparing the year.

Disables December 25th.

Datetime with December 25th disabled

<ion-datetime></ion-datetime>
Enter fullscreen mode Exit fullscreen mode
import { getDate, getMonth, getYear } from 'date-fns';

const isDateEnabled = (dateIsoString: string) => {
  const date = new Date(dateIsoString);
  if (getDate(date) === 25 && getMonth(date) === 11) {
    return false;
  }
  return true;
};

const datetime = document.querySelector('ion-datetime');
datetime.isDateEnabled = isDateEnabled;
Enter fullscreen mode Exit fullscreen mode

We hope that you find value in this feature and others in the 6.1.0 release.

Top comments (0)