Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datetimepicker plugin selects one hour back after selecting particular time

i am using datetimepicker plugin.I am getting a strange error. whenever i am selecting a time from the dropdown ,the plugin selects one hour before of the selected time. Couldn't figure out why this is happening

<!DOCTYPE html>
<html>
<head>
	 <title> Date Time Picker </title>
	 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.4/build/jquery.datetimepicker.min.css" />
</head>
<body>

    <input type="text" value="2:00 PM" id="timepicker" />

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.4/build/jquery.datetimepicker.full.min.js" ></script>
    <script>
        $('#timepicker').datetimepicker({
            datepicker: false,
            step:30,
            format:'g:i A'
        });
    </script>
</body>
</html>

The answers i got this post is to Change the g in the format option to Capital G. but when i changed like this means 12 hours format has been changed to 24 hour format.I want to have it to retain 12 hours format and solve this issue

like image 901
yasarui Avatar asked Jan 03 '23 19:01

yasarui


2 Answers

Just change format g:i A to H:i A:

<!DOCTYPE html>
<html>
<head>
	 <title> Date Time Picker </title>
	 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.4/build/jquery.datetimepicker.min.css" />
</head>
<body>

    <input type="text" value="2:00 AM" id="timepicker" />

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.4/build/jquery.datetimepicker.full.min.js" ></script>
    <script>
        $('#timepicker').datetimepicker({
            datepicker: false,
            step:30,
            format:'H:i A'
        });
       $('#timepicker').change(function(){
           $(this).val($(this).val().replace(/^0+/, ''));
       });
    </script>
</body>
</html>
like image 157
Govind Samrow Avatar answered Jan 06 '23 11:01

Govind Samrow


It is because you are using invalid format. Just use format:'H:i A' instead of format:'g:i A'

$('#timepicker').datetimepicker({
  datepicker: false,
  step: 30,
  format:'H:i A'
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.4/build/jquery.datetimepicker.min.css" />

<input type="text" value="2:00 AM" id="timepicker" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.4/build/jquery.datetimepicker.full.min.js"></script>
like image 45
Rohan Kumar Avatar answered Jan 06 '23 13:01

Rohan Kumar