6

I am giving an input field with type datetime-local,

<input type="datetime-local" class="form-control" name="booking_checkin">

In which after filling and viewing it, the format is like this,

2017-08-06T02:32 .

It looks like awkward and i need this pattern to be changed,

I would like to have format like this,

2017-08-06, 02:32.

I apologise for not posting what i have tried because i don't even have a start up idea to get it after searching a lot here.. Kindly help me to solve it..

Maniraj Murugan
  • 6,412
  • 10
  • 56
  • 92
  • You can find your answer [here](https://stackoverflow.com/questions/10854874/input-type-datetime-value-format). Thanks – Rahul Parikh Aug 07 '17 at 10:51
  • You're not being clear, are you getting that string when you fetch the elements value? If so, it's a valid date that you can pass to `new Date` to get anything you want – adeneo Aug 07 '17 at 10:55
  • On which browser do you get the described behavior? – svet Feb 26 '21 at 18:23

2 Answers2

0

Change date format on server-side

    <?php 
$date = date('Y-m-d, h:i',strtotime($_POST['booking_checkin']));
?>
perumal N
  • 436
  • 1
  • 10
  • 19
0

When getting the value, it's valid date string, and you can pass it to new Date and then parse the individual values anyway you'd 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 one parses a date

adeneo
  • 293,187
  • 26
  • 361
  • 361
  • Date and time i will fill it, but when i am viewing it, the format is like this ```2017-08-06T02:32``` which i need to change like this ```2017-08-06, 02:32``` . To explain in clear i am filling out the date and time as like the input given by you in the answer and i also submitted the form but the format it displays the date and time need to be changed.. – Maniraj Murugan Aug 07 '17 at 11:06
  • The format in the input itself is as it says local. I'm getting `01.12.2017 01:01`, I haven't heard anyone getting an UTC string in the input, so I assumed that was the fetched value, which you change with javascript – adeneo Aug 07 '17 at 12:49
  • I am getting a continuous pattern with letter ```T``` in middle which i don't need here.. If i assign ```input type="datetime-local"``` i am getting output like ```2017-08-06T02:32``` this only.. – Maniraj Murugan Aug 07 '17 at 12:53
  • Yes, that's a valid date string, if you want to change it, either change it on the clientside before the data is submitted or change it on the serverside, which most of the time also supports that format. You can't make that input return anything else from getting the value – adeneo Aug 07 '17 at 12:59