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.

+3


source to share


2 answers


Change the format date

on the server side



    <?php 
$date = date('Y-m-d, h:i',strtotime($_POST['booking_checkin']));
?>

      

0


source


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">
      

Run codeHide result


Using jQuery for simplicity, it has nothing to do with how the date is parsed once

0


source







All Articles