How can I change the input format of type datetime-local?
I am giving an input field with a type datetime-local
,
<input type="datetime-local" class="form-control" name="booking_checkin">
In which, after filling and viewing, its format looks like this:
2017-08-06T02:32
...
It looks awkward and I need to change this template,
I would like to have a format like this,
2017-08-06, 02:32
...
I apologize for not posting what I tried because I don't even have a launch idea to get it after a lot of searching here. Please help me to solve this.
source to share
When getting a value, it is a valid date string and you can pass it to new Date
and then parse the individual values ββanyway you would like
$('.form-control').on('change', function() {
var parsed = new Date(this.value);
var ten = function(x) { return x < 10 ? '0'+x : x};
var date = parsed.getFullYear() + '-' + (parsed.getMonth() + 1) + '-' + parsed.getDate();
var time = ten( parsed.getHours() ) + ':' + ten( parsed.getMinutes() );
console.log( date + ', ' + time)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="datetime-local" class="form-control" name="booking_checkin">
Using jQuery for simplicity, it has nothing to do with how the date is parsed once
source to share