DEV Community

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

Posted on

2 2

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay