93

Date picker landed on Chrome 20, is there any attribute to disable it? My entire system uses jQuery UI datepicker and it's crossbrowser, so, I want to disable Chrome native datepicker. Is there any tag attribute?

Slava Fomin II
  • 26,865
  • 29
  • 124
  • 202
Alexandre
  • 7,004
  • 5
  • 54
  • 72
  • 5
    possible duplicate of [How can I disable the new Chrome HTML5 date input?](http://stackoverflow.com/questions/11270675/how-can-i-disable-the-new-chrome-html5-date-input) – Wesley Murch Jul 12 '12 at 19:20

14 Answers14

148

To hide the arrow:

input::-webkit-calendar-picker-indicator{
    display: none;
}

And to hide the prompt:

input[type="date"]::-webkit-input-placeholder{ 
    visibility: hidden !important;
}
Yacine Zalouani
  • 7,999
  • 6
  • 25
  • 24
57

If you have misused <input type="date" /> you can probably use:

$('input[type="date"]').attr('type','text');

after they have loaded to turn them into text inputs. You'll need to attach your custom datepicker first:

$('input[type="date"]').datepicker().attr('type','text');

Or you could give them a class:

$('input[type="date"]').addClass('date').attr('type','text');
maxpower9000
  • 223
  • 2
  • 8
rjmunro
  • 27,203
  • 20
  • 110
  • 132
  • 5
    Easily my favorite solution. It is simple and does not implement browser specific CSS selectors. The intended result, IMO, should be to prevent the native control if JavaScript is enabled, allowing for a custom date picker. However, if JavaScript is disabled then the native controls still work. Honestly, this should be the accepted answer. – Justin Bull Mar 27 '13 at 20:18
  • I believe that some (if not all) versions of IE don't allow you to switch the type of an input. Sure, this question is about Chrome, but one would think that one would want some consistency cross-browser... – Travesty3 Oct 31 '13 at 19:48
  • 2
    @travesty3 do those versions of IE support html5 datepickers, or just fall back to text? If not, then it doesn't matter. – rjmunro Nov 01 '13 at 08:49
  • @rjmunro: True. It looks like IE9+ does allow you to switch the type. Guess I'm just being picky. I ran into this problem when making a workaround for browsers that don't support `placeholder` (set default value and clear on focus, but doesn't work for password fields since the placeholder text is bulleted). – Travesty3 Nov 01 '13 at 19:29
  • @rjmunro: using .prop() on an attribute should be discouraged. Better to use `$('input[type="date"]').attr('type','text');` See http://api.jquery.com/prop/ Quote: The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while **.attr() retrieves attributes**. – maxpower9000 Jun 11 '15 at 12:46
  • 2
    I don't understand why using for a date field is misusing it? (first sentence of this answer) – Steve Aug 18 '15 at 01:01
  • @steve `` means show a browser native date picker, like `` means show a browser native checkbox. The OP doesn't want a browser date picker, so shouldn't be using `type="date"`. – rjmunro Aug 19 '15 at 10:12
  • @Steve Of course, it's much better to use the browser native date picker wherever possible, as it is different on different platforms (especially mobile), and works how end users are used to. It also uses the correct locale for month names etc., and is accessible. However, in my experience designers often think they can make a better date picker than the standard one, or they want things to look the same for different platforms. This is IMHO terrible for usability across sites. There are huge numbers of websites with not-quite-working-how-you'd-expect date pickers out there. – rjmunro Aug 19 '15 at 10:15
  • ... and for those working in MS MVC, removing the [DataType(DataType.Date)] annotation in the view model also helps – netfed Jun 07 '17 at 03:12
  • 3
    It is perfectly valid to not want a calendar date picker for a field like Date of Birth, but to still want the field validated by native browser. – Duncanmoo Aug 27 '17 at 08:56
  • This is actually the better option, as mobile browsers sometimes load its own date picker. Change the field to text, avoids that as well. Thanks mate. – Harpreet Singh Aug 24 '20 at 07:58
  • 1
    @Duncanmoo What's special about dates of birth? Just set the max to today (if you want allow babies to be entered on the day they are born), or N years ago where N is the minimum age for users of your site. Especially on mobile, I'd rather use the browser native date picker to enter my date of birth than some custom JS thing. Also how many custom JS date picker implementations exist that don't support validation? – rjmunro Aug 24 '20 at 12:05
  • I disagree with the assertion that you should only use `` if you want to use the browser's native datepicker - it's more semantically correct to use it when you want a date from the user, regardless of how you want that input to behave. Autofill will work better (especially in Chromium, which ignores what you tell it otherwise), assistive technologies understand it better, and it carries some built-in validation. If we want to be semantically correct but still provide a consistent experience, this type of workaround is necessary. – FireSBurnsmuP May 27 '21 at 14:56
16

You could use:

jQuery('input[type="date"]').live('click', function(e) {e.preventDefault();}).datepicker();
Jimbo
  • 364
  • 1
  • 8
  • 3
    if you use jQuery >= 1.7: `jQuery('input[type="date"]').on('click', function(e) {e.preventDefault();}).datepicker();` – yorch Mar 21 '13 at 19:22
  • 1
    This answer does not remove the native styling. http://stackoverflow.com/a/11418704/229787 – Justin Bull Mar 27 '13 at 20:28
  • 5
    @JustinBull true but the question wasn't about removing styling but allowing the jQuery date picker to be used instead which this answer achieves. Any ideas how the styling can be removed as well? – Jimbo Mar 28 '13 at 09:24
7

With Modernizr (http://modernizr.com/), it can check for that functionality. Then you can tie it to the boolean it returns:

// does not trigger in Chrome, as the date Modernizr detects the date functionality.
if (!Modernizr.inputtypes.date) { 
    $("#opp-date").datepicker();
}
jrummell
  • 42,637
  • 17
  • 112
  • 171
CodeFinity
  • 1,142
  • 2
  • 19
  • 19
  • 3
    This is pretty useful for falling back to jQuery datepicker when a native datepicker isn't available. However, this question looks to be about getting rid of Chrome's date picker, so I've voted your answer down. – Sam Sep 08 '14 at 03:27
7

This worked for me

input[type=date]::-webkit-inner-spin-button, 
input[type=date]::-webkit-outer-spin-button { 
  -webkit-appearance: none; 
  margin: 0; 
}
Mantisimo
  • 4,203
  • 5
  • 37
  • 55
2

The code above doesn't set the value of the input element nor does it fire a change event. The code below works in Chrome and Firefox (not tested in other browsers):

$('input[type="date"]').click(function(e){
     e.preventDefault();
}).datepicker({
    onSelect: function(dateText){
        var d = new Date(dateText),
        dv = d.getFullYear().toString().pad(4)+'-'+(d.getMonth()+1).toString().pad(2)+'-'+d.getDate().toString().pad(2);
        $(this).val(dv).trigger('change');
    }
});

pad is a simple custom String method to pad strings with zeros (required)

MartijnR
  • 31
  • 4
1

I agree with Robertc, the best way is not to use type=date but my JQuery Mobile Datepicker plugin uses it. So I have to make some changes:

I'm using jquery.ui.datepicker.mobile.js and made this changes:

From (line 51)

$( "input[type='date'], input:jqmData(type='date')" ).each(function(){

to

$( "input[plug='date'], input:jqmData(plug='date')" ).each(function(){

and in the form use, type text and add the var plug:

<input type="text" plug="date" name="date" id="date" value="">
ebelendez
  • 848
  • 2
  • 12
  • 22
  • 3
    If you want to add custom attributes for local script data, the valid way is to use [`data-*` attributes](http://html5doctor.com/html5-custom-data-attributes/). In this case, call your attribute `data-plug` instead of `plug`, it is then guaranteed to never clash with any new attribute added in HTML6/7/8/etc. – robertc Mar 13 '13 at 12:10
1

I use the following (coffeescript):

$ ->
  # disable chrome's html5 datepicker
  for datefield in $('input[data-datepicker=true]')
    $(datefield).attr('type', 'text')
  # enable custom datepicker
  $('input[data-datepicker=true]').datepicker()

which gets converted to plain javascript:

(function() {
  $(function() {
    var datefield, _i, _len, _ref;
    _ref = $('input[data-datepicker=true]');
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      datefield = _ref[_i];
      $(datefield).attr('type', 'text');
    }
    return $('input[data-datepicker=true]').datepicker();
  }); 
}).call(this);
Paolo Dona
  • 29
  • 3
1

This worked for me:

// Hide Chrome datetime picker
$('input[type="datetime-local"]').attr('type', 'text');

// Reset date values
$("#EffectiveDate").val('@Model.EffectiveDate.ToShortDateString()');
$("#TerminationDate").val('@Model.TerminationDate.ToShortDateString()');

Even though the value of the date fields was still there and correct, it did not display. That's why I reset the date values from my view model.

Cogitator
  • 154
  • 1
  • 1
0

This works for me:

;
(function ($) {
    $(document).ready(function (event) {
        $(document).on('click input', 'input[type="date"], input[type="text"].date-picker', function (e) {
            var $this = $(this);
            $this.prop('type', 'text').datepicker({
                showOtherMonths: true,
                selectOtherMonths: true,
                showButtonPanel: true,
                changeMonth: true,
                changeYear: true,
                dateFormat: 'yy-mm-dd',
                showWeek: true,
                firstDay: 1
            });

            setTimeout(function() {
                $this.datepicker('show');
            }, 1);
        });
    });
})(jQuery, jQuery.ui);

See also:

How can I disable the new Chrome HTML5 date input?

http://zizhujy.com/blog/post/2014/09/18/Add-date-picker-to-dynamically-generated-input-elements.aspx

Community
  • 1
  • 1
Jeff Tian
  • 5,210
  • 3
  • 51
  • 71
0

I know this is an old question now, but I just landed here looking for information about this so somebody else might too.

You can use Modernizr to detect whether the browser supports HTML5 input types, like 'date'. If it does, those controls will use the native behaviour to display things like datepickers. If it doesn't, you can specify what script should be used to display your own datepicker. Works well for me!

I added jquery-ui and Modernizr to my page, then added this code:

<script type="text/javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(initDateControls);
    initDateControls();

    function initDateControls() {
        //Adds a datepicker to inputs with a type of 'date' when the browser doesn't support that HTML5 tag (type=date)'
        Modernizr.load({
            test: Modernizr.inputtypes.datetime,
            nope: "scripts/jquery-ui.min.js",
            callback: function () {
                $("input[type=date]").datepicker({ dateFormat: "dd-mm-yy" });
            }
        });
    }

</script>

This means that the native datepicker is displayed in Chrome, and the jquery-ui one is displayed in IE.

Philip Stratford
  • 4,513
  • 4
  • 45
  • 71
0

For Laravel5 Since one uses

{!! Form::input('date', 'date_start', null , ['class' => 'form-control', 'id' => 'date_start', 'name' => 'date_start']) !!}

=> Chrome will force its datepicker. => if you take it away with css you will get the usual formatting errors!! (The specified value does not conform to the required format, "yyyy-MM-dd".)

SOLUTION:

$('input[type="date"]').attr('type','text');

$("#date_start").datepicker({
    autoclose: true,
    todayHighlight: true,
    dateFormat: 'dd/mm/yy',
    changeMonth: true,
    changeYear: true,
    viewMode: 'months'
});
$("#date_stop").datepicker({
    autoclose: true,
    todayHighlight: true,
    dateFormat: 'dd/mm/yy',
    changeMonth: true,
    changeYear: true,
    viewMode: 'months'
});
$( "#date_start" ).datepicker().datepicker("setDate", "1d");
$( "#date_stop" ).datepicker().datepicker("setDate", '2d');
0

in HTML use prop "min"

    <input type="date" class="form-control" [min]="getDate()">

and in-class use this fun to get today's date.

   getDate(): string {
    return new Date().toISOString().split('T')[0];
   }
endo.anaconda
  • 2,449
  • 4
  • 29
  • 55
Sherif Fahd
  • 91
  • 1
  • 2
0

Not sure if i'm missing a trick or not here, but here goes:

I changed the type of the underlying input field from date to text. The effectivly disabled the native control and allowed a custom date time picker to show correctly.

<input type="text" id="Demo">

I was happy enough

PowerMan2015
  • 1,307
  • 5
  • 19
  • 40