Problem/Motivation

Datetime field limits are currently hard-coded in Core.

While this may seem helpful to prevent long select lists (#2836054: Allow configuring of datelist year range date_year_range via UI) it's a bug for HTML datepicker input fields, where such a limitation can block valid use-cases, e.g. entering a date < 1900.
Furthermore, years go on and having a hard-coded limit in code is maintenance.

https://git.drupalcode.org/search?search=%23date_year_range&nav_source=n...

Steps to reproduce

  1. Add a datetime field to an entity type
  2. Enter a date <= 1899 or >= 2051 and see a form validation error - no way to save the entity
  3. Find no way to alter these limits in the UI

Proposed resolution

Make the Date and Date Range configurable, to have a MINIMUM and a MAXIMUM allowed date to show in the frontend.
Both fields should support absolute and relative dates.
User should be aware that if using a list (select) widget having list with very many entries can slow down browsers

This will allow custom date intervals like the ones below:

  • Show only the dates between last month (Present minus 1 month) and 1 week from now (Present plus 7 days).
  • If we wanted to show a date range from 2000 until yesterday we could do that by excluding the current day (Present) because the date for "yesterday" changes everyday.

Use cases:

  • In a birthdate field, never show (never allow) dates from the future.
  • Allow entering a birthdate < 1900

#2836054: Allow configuring of datelist year range date_year_range via UI already has a related MR!

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-2942828

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

dalra created an issue. See original summary.

dalra’s picture

Project: DateTime » Drupal core
Version: » 8.6.x-dev
Component: Code » datetime.module
mpdonadio’s picture

Not sure if this falls under the "do-it-all" use case we are trying to avoid in core, but this would be a great addition for DateTime Extras.

dalra’s picture

This is actually required by law in the US and in most other countres.
In Europe it is included in the General Data Protection Regulation (GDPR) - Enforcement date: 25 May 2018.

eugdpr.org/gdpr-faqs.html

What about Data Subjects under the age of 16?

Parental consent will be required to process the personal data of children under the age of 16 for online services; member states may legislate for a lower age of consent but this will not be below the age of 13.

So what's this have to do with Drupal date?

We need to make sure we do not allow user registration below the age of 13.

But the date field is not restricted to certain values and allows the user to enter any value.
By allowing that, the site owner is KNOWINGLY BREAKING THE LAW and risking heavy fines (because nothing is stopping children under 13 from registering). Now, any birth date is accepted.

By having the option to set a custom interval, we could create a birth date field like this one:

1970 to (Present minus 13 years)

Only the birth dates in this interval should be selectable (and allowed) by the user in the front-end. It allows the registration of those who are 13 today but it prevents it for those who will be 13 tomorrow.

At the moment, the date range cannot be changed and users can choose dates between 1970 and 2038.
This interval is completely inadequate for most cases.
For example, users now can select 2038 as their year of birth.

dalra’s picture

Issue summary: View changes
mpdonadio’s picture

Age compliance can be implemented in contrib and or in custom code in numerous ways right now. The datetime and daterange elements both have `#date_year_range` attributes that can be added (via alters) where needed (though I think that just sets min/max attributes on the elements. And, adding your own validation functions can also be done with alters.

I am not saying this isn't a useful feature, but I do not see where this becomes a mandatory for Drupal core to implement this feature on all datetime, daterange, and timestamp fields regardless of how they are being used.

dalra’s picture

Well, if my suggestion with the Present sounds too complicated to be in core, would you agree that at least the option to manually narrow down the date range is needed?

I am talking only about the daterange element here.

I propose to have an option to set a minimum and maximum allowed date because in most cases, the default interval 1970-2038 is too broad.

For example, if we need to collect some dates between 2000 and 2001, it does not make sense to show to the users in the dropdown menu all the years from 1970 to 2038.
Some users will just enter bad data.

I agree that it could be done with a custom module plus validation, but at least this minimum and maximum allowed date instead of 1970 and 2038 would be what most people want.

akmalfikri’s picture

Is there any temporary solution for this, eg on JS or client side?

michiellucas’s picture

You can use this contrib module with the provided patch to configure the year reange

wturrell’s picture

@michiellucas - I'm guessing you meant to link to DateTime Extras?

@akmalfikri - try something like this in a custom module inside a HOOK_form_alter method (match the method to the correct form(s):

$form['field_dob']['widget'][0]['value']['#date_year_range'] = '1930:-13';

A slightly annoying thing about `#date_year_range`: while it can do relative dates (as above), you apparently can't reverse the year order by swapping the numbers around? (anecdotally quite a common request, e.g. Date of Birth fields).

I found some code that does this by hooking into template_preprocess_select and grabbing, reversing the array.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

berdir’s picture

Yeah, reverse would definitely be very helpful, just landed here as well when searching for an existing issue about that.

+1 on exposing the year range setting in the widget that seems simple enough, don't want to add another contrib module just for that so I'm currently using custom code for that.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mygumbo’s picture

What is the status of getting this into core, please?

pieterdc’s picture

Thanks @wturrell for your hint to @akmalfikri, it has proven to be useful to me as well.
On top of that, I used #3269890: #date_year_range is not validated server-side to enforce the year range on server-side validation (instead of only client-side, via HTML 5).

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jaime@gingerrobot.com’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

anybody’s picture

Title: Make the Date and Date Range configurable to allow MIN and MAX dates, only past or only future dates, or both » Remove hard-coded #date_year_range making it impossible to enter dates > 1900, > 2050. Instead make them optional and configurable
Category: Feature request » Bug report
Priority: Normal » Major
Issue summary: View changes
Issue tags: +Bug Smash Initiative, +Site builder experience (SX)

I totally agree with @berdir in #12.
It's a not a good decision and assumption for a popular (not only developer) CMS like Drupal to set a hard-coded date limit in code, which non-developers can not change easily. Having dates outside of that area is absolutely possible!

See https://git.drupalcode.org/search?search=%23date_year_range&nav_source=n...

Some examples:
- Using Drupal for genealogy or other historical dates (< 1900)
- Using Drupal for birthdates (>= today doesn't make sense)

I updated the issue summary and changed this to a Bug report because of the reasons given above.

I see two possible solutions here:

  1. Remove the hard-coded limit and make this a feature-request again
  2. Add a UI to set the limit and set the current limits as defaults if needed for BC

I'd vote for (2) because I think (1) would at least resolve the bug but wouldn't solve the need for a better UX solution at the same time.

Please note the various related issues around.

I adjusted the issue summary accordingly and added the template.

anybody’s picture

Title: Remove hard-coded #date_year_range making it impossible to enter dates > 1900, > 2050. Instead make them optional and configurable » Remove hard-coded #date_year_range making it impossible to enter dates < 1900, > 2050. Instead make them optional and configurable

anybody’s picture

Title: Remove hard-coded #date_year_range making it impossible to enter dates < 1900, > 2050. Instead make them optional and configurable » Remove hard-coded #date_year_range making it impossible to enter dates < 1900, > 2050. Instead make them optional and configurable in the UI
anybody’s picture

anybody’s picture

Issue summary: View changes