0

I have several inputs with dates. I don't need time, but somehow I can't get rid of it.

I tried all the solutions I found on StackOverflow without success.

I am using the tempusdominus datetimepicker.

JS FIDDLE LIVE DEMO

Does anyone know how can I remove the option to enter hours and minutes?

I thought format: 'MM/DD/YYYY' would do the trick but apparently it's not working.

$('#monthDatetimepicker').datetimepicker({
            defaultDate: date,
            viewMode: 'days',
            format: 'MM/DD/YYYY'
        });
Cheknov
  • 1,892
  • 6
  • 28
  • 55
  • This j Query code makes my eyes hurt. What's the variable for displaying the value in the table? Can you point me to it please ? – ZombieChowder Sep 17 '21 at 10:56
  • It's the second one `stock[1]` – Cheknov Sep 17 '21 at 11:02
  • Don't use a `datetimepicker` but use a `datepicker` instead? – freedomn-m Sep 17 '21 at 11:24
  • You've attempted to set the format, but you've applied it to *nothing*. `console.log($("#monthDatetimepicker").length)` == 0. There's nothing with id monthDatetimepicker *at the time you call the options* - nor even after you build the table as it's `monthDatetimepicker0` (etc) – freedomn-m Sep 17 '21 at 11:30

2 Answers2

1

You need to set the default options so that it applies to newly created date[time]pickers:

From the options page on how to set global defaults:

  $.fn.datetimepicker.Constructor.Default = $.extend({}, $.fn.datetimepicker.Constructor.Default, {
    viewMode: 'days',
    format: 'MM/DD/YYYY',
  });

Updated fiddle


Your issue was that this line

$('#monthDatetimepicker').datetimepicker({
    format: 'MM/dd/YYYY'

doesn't get applied to anything as $('#monthDatetimepicker') === 0 at the time the code runs.

Moving that line to the end also does nothing as your newly created date inputs have id="monthDatetimepicker" + i. Changing to a valid ID such as:

$('#monthDatetimepicker0').datetimepicker({
    format: 'MM/dd/YYYY'

then only works for the first picker.

So you could do some hideous $("[id=^monthDatetimepicker]").datetimepicker... or, easier, you could add a class into your html builder and use that as a selector. Or set it globally as above (which will affect other pickers unless they had a different format applied). (or use a datepicker instead...)

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
0

Add .substring(0,10) at the end.

John Shelby
  • 87
  • 2
  • 9
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 17 '21 at 11:15