Bootstrap datepicker hours, minutes, seconds for datetime mysql DEMO?

Bootstrap datepicker hours, minutes, seconds for datetime mysql DEMO?

Here is a Bootstrap script, for datepicker, hours, minutes, seconds for datetime.

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

Use this javascript code from below.

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();
 });

I have used this script on quite a big project and it worked fine on all the important browsers, Chrome, Firefox and Microsoft Edge.

Hopefully it will work for you as well.

If you have any questions, leave in the comments section.

Add a comment: