0

I have a datetime-local input (and jQuery) and I'm trying to figure out how I can essentially autofill the time of the input. The value is not required so I can't just set a default datetime, but I need the time to default to a predetermined time (let's say 10:00 PM) if a user picks a date. (Other times are valid but extremely rare)

I tried to pull the incomplete value, but that appears to be impossible due to W3C's sanitation of the value (per this SO thread and testing)

Is this even possible or will I need to abandon datetime-local?

Sleepwave
  • 103
  • 3
  • Then you'll need to use a [custom date picker](https://plugins.jquery.com/tag/datepicker/), which pretty much everyone does anyway because native date pickers either can't be styled to match the UI or aren't supported at all. You can capture keys as they are pressed, but I think that way madness lies… – RobG Mar 19 '20 at 06:21

1 Answers1

0

you can set it as value

var selectElement = $('#date-time');
var date = '2020-03-19'; // default date

selectElement.on('change', (event) => {
    date = selectElement.val().split('T')[0];
    selectElement.attr('value', date+'T22:00"');
});
S K R
  • 552
  • 1
  • 3
  • 16
  • This doesn't answer the OP's question. They want to complete the date if it's only partially keyed in, in which case `selectElement.val()` will return an empty string. Also, the change event isn't dispatched until the entire value has been entered. – RobG Mar 19 '20 at 06:23