0

We recently upgraded an application from Bootstrap 4 to 5. Since the upgrade, users are unable to paste into <input type="date"... controls.

I scoured the Bootstrap 5.3.x documentation (forms section) and I cannot find any documentation about dates or how they are supposed to work. I have reviewed the entire Bootstrap JS and CSS and did not find anything about "clip", "paste", "date", etc.

For example, in this "Start Date", I can type into it and use the calendar UI, but if I copy a date into my clipboard like "12/12/2024" and paste, nothing happens.

HTML date input control formatted mm/dd/yyyy with "Start" label and calendar

I can hit Ctrl-A to select the date, but paste does not work in that state either.

Is there some way to support pasting into a date input?

I discovered that paste is missing in my context menu, so maybe that is a clue. This might be a default of HTML date input. The same problem exists on MDN.

3
  • 2
    FYI: on getbootstrap.com/docs/5.3/content/reboot/#forms (search for "Example date") I cannot paste either Commented Jul 1 at 14:28
  • This question is similar to: Enable copy/paste on html5 date field. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 3 at 7:56
  • 1
    it worked in bs 4 because you probably used some date picker, so you might check the migration process and if so, see if it's compatible with bs 5 and restore it Commented Jul 3 at 8:04

1 Answer 1

-1

I found a way!

The code changes the input temporarily to a text input so you can copy and/or paste as needed.

This code supports pasting as MM/DD/YYYY and YYYY-MM-DD formats.

This could be improved by using Luxon date parsing.

document.addEventListener("DOMContentLoaded", function () {
    var isDate = function(date) {
      return (new Date(date) !== "Invalid Date") && !isNaN(new Date(date));
    }
    document.addEventListener("keydown", function (e) {
        if (
            e.target.matches('input[type="date"]') &&
            e.ctrlKey === true &&
            (e.key === "c" || e.key === "C" || e.keyCode === 67)
        ) {
            // Temporarily change to text to allow selection/copy
            const input = e.target;
            const prevType = input.type;
            input.type = "text";
            input.select();
            document.execCommand("copy");
            input.type = prevType;
            // Prevent default copy behavior
            e.preventDefault();
        }
    });

    document.addEventListener("paste", function (e) {
        const active = document.activeElement;
        if (active && active.type === "date") {
            // Get pasted text and set as value
            const pasted = (e.clipboardData || window.clipboardData).getData('text');
            if (isDate(pasted)) {
                active.value = (new Date(pasted)).toISOString().slice(0,10); //return YYYY-MM-DD;
            }
            // Prevent default paste behavior
            e.preventDefault();
        }
    });
});
<input type="date" value="2017-06-01" />

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.