Bootstrap datepicker hours, minutes, seconds for datetime mysql DEMO

This code will add hours / minutes / seconds in the datetime format of mysql.

Use this javascript code. And use this script as example, it has a full page with cdn includes, html and javascript.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// JavaScript Document
$(function () {
   var bindDatePicker = function() {
		$(".date").datetimepicker({
			format:'YYYY-MM-DD HH:mm:ss',
			icons: {
				time: "fa fa-clock-o",
				date: "fa fa-calendar",
				up: "fa fa-arrow-up",
				down: "fa fa-arrow-down"
			}
		}).find('input:first').on("blur",function () {
			// check if the date is correct. We can accept dd-mm-yyyy and yyyy-mm-dd.
			// update the format if it's yyyy-mm-dd
			var date = parseDate($(this).val());
 
			if (! isValidDate(date)) {
				//create date based on momentjs (we have that)
				date = moment().format('YYYY-MM-DD HH:mm:ss');
			}
 
			$(this).val(date);
		});
	}
 
   var isValidDate = function(value, format) {
		format = format || false;
		// lets parse the date to the best of our knowledge
		if (format) {
			value = parseDate(value);
		}
 
		var timestamp = Date.parse(value);
 
		return isNaN(timestamp) == false;
   }
 
   var parseDate = function(value) {
		var m = value.match(/^(\d{1,2})(\/|-)?(\d{1,2})(\/|-)?(\d{4})$/);
		if (m)
			value = m[5] + '-' + ("00" + m[3]).slice(-2) + '-' + ("00" + m[1]).slice(-2);
			var h = d.getHours();
        h = (h < 10) ? ("0" + h) : h ;
 
        var m = d.getMinutes();
        m = (m < 10) ? ("0" + m) : m ;
 
		var s = d.getSeconds();
        s = (s < 10) ? ("0" + s) : s ;
 
		return value;
   }
 
   bindDatePicker();
 });

ADD A COMMENT: