15

I've searched high and low for an answer to my question but haven't found one, I did find two related questions though: JQuery Datepicker should display date only , jQuery UI DateTimepicker - cannot hide the time

But they didn't solve my problem.

I'm using Trent Richardsons datetimepicker and I would like it to show only date in the textbox. The answer to why I'm not using the datepicker without the addon is because sometimes I want it to show datetime and sometimes only date.

I've can hide the timepicker with the following code:

    datetimepicker("option", "showTimepicker", false);

The time disappears from my textbox but the date has a trailing space, "2013-11-01 " I've tried the option for separator but no luck.

    datetimepicker("option", "separator", "");

Anyone got an idea how I can solve this?

A bit clarification, I'm trying to write kind of a component that can be used through a large solution where it sometimes needs to be a datetimepicker and sometimes a datepicker. This "component" contains an input (my datetimepicker) that is wrapped in some html. It also has properties that can be set, one of them is the SetDateOnly(), and that is where I'm trying to get the datepicker to show only date.

Community
  • 1
  • 1
Robert Persson
  • 181
  • 1
  • 1
  • 8
  • How about just using datepicker? – Travis J Nov 11 '13 at 15:14
  • 1
    Create a jsfiddle to make it easier for us to solve the problem. – Rick Suggs Nov 11 '13 at 15:19
  • I don know how to create a jsfiddle, javascripts isn't really not my best language.. – Robert Persson Nov 11 '13 at 15:31
  • So by saying javascript isn't really not your best language, if you cancel out the double negative you're saying it IS your best language? Just go to jsfiddle.net and fill out the html, javascript, and css sections to create an example of your code that anyone can see and inspect. It's very easy. – Rick Suggs Nov 11 '13 at 16:39

13 Answers13

20

Try this options:

$("#selector").datetimepicker({
  minView: 2,
  format: 'YYYY-MM-DD'
});
Ibanêz
  • 349
  • 2
  • 8
12

Then you can go with showTimePicker like this:-

On Form Load

$(document).ready(function(){
    $('#selector').datetimepicker({
        'showTimepicker': false
    });     
});

On run time

$(".btn").click(function(){ 
    $('#selector').datetimepicker("destroy"); //destroys the previous settings.
    $('#selector').datetimepicker({
        'showTimepicker': false
    });                     
});
GDG
  • 195
  • 1
  • 10
5
$(document).ready(function(){
    $('#selector').datetimepicker({
        viewMode: 'days',
        format: 'DD/MM/YYYY'
    });     
});

It works like this.

bummi
  • 26,435
  • 13
  • 58
  • 97
Corrot
  • 251
  • 5
  • 3
  • 1
    FYI, this is how you do it if you're using ['bootstrap-datetimepicker' by eonasdan on GitHub](https://eonasdan.github.io/bootstrap-datetimepicker/). – Vikram Deshmukh Oct 02 '15 at 07:01
3

This is working for me :)

    $("#selector").datetimepicker({
    minView: 2,
    format: 'dd-mm-yyyy',
    autoclose: true
    });
Ashish Thakur
  • 173
  • 12
3

if another code not work then you can use this code

    $('#datetimepicker_date_to_view').datetimepicker({
            timepicker:false,
            format:'d.m.Y',
        }).on('change', function() {
            $('.xdsoft_datetimepicker').hide();
            var str = $(this).val();
            str = str.split(".");
            $('#alt_date_field').val(str[2]+'-'+str[1]+'-'+str[0]);
        });
Sandeep Sherpur
  • 1,507
  • 18
  • 18
2

Why not like this:

$('#selector').datetimepicker(); // When required both functionalities.
$('#selector').timepicker(); // When only time is required.
$('#selector').datepicker(); // Otherwise regular datepicker can be used.
GDG
  • 195
  • 1
  • 10
  • Yes, I've been thinking about doing it this way and hide/show datepicker/datetimepicker depending on what is needed but I figured it should be possible to set the datetimepicker to show only date. – Robert Persson Nov 11 '13 at 16:00
2

This is my solution:

  $("#Time").datetimepicker({
      datepicker: false,
      step: 15,
      format: 'g:i a',
    });

Using jQuery DateTimePicker plugin v2.3.8

Gabriel Reguly
  • 190
  • 1
  • 8
2

As per the documentation it is:

$("#selector").datetimepicker({
  format: 'L'
});
willer2k
  • 426
  • 1
  • 5
  • 13
1

I tried this. It's working for me. The following format parameter has to set under the datetimepicker like this 'YYYY-MM-DD'

$(document).ready(function(){
  $('#selector').datetimepicker({         
    format: 'YYYY-MM-DD'
  });     
});`
ANIL PATEL
  • 389
  • 2
  • 10
0

I solved my problem by having a parameter to my "component" saying if it should contain a datetimepicker or a datepicker. The drawback is that I can't switch between them as easily as I intended to do.

Robert Persson
  • 181
  • 1
  • 1
  • 8
0
        <script type="text/javascript">
            $(function () {
                $('#selector').datetimepicker({
                  format: 'YYYY-MM-DD'
                });
            });
        </script>
-1
$('#datetimepicker1').datetimepicker({
    viewMode: 'days',
    format: 'DD/MM/YYYY',
  icons: {
      time: 'fa fa-clock-o',
      date: 'fa fa-calendar',
      up: 'fa fa-chevron-up',
      down: 'fa fa-chevron-down',
      previous: 'fa fa-chevron-left',
      next: 'fa fa-chevron-right',
      today: 'fa fa-crosshairs',
      clear: 'fa fa-trash'
    }
});
Peter O.
  • 28,965
  • 14
  • 72
  • 87
-2

I tried this.. it's working for me.

use datepicker() instead of datetimepicker()

Ref: jQuery UI DateTimepicker - cannot hide the time