Git Product home page Git Product logo

dtimepicker's People

Contributors

dmuy avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

libayiv

dtimepicker's Issues

When you put 10:00 whether pm or am it puts a 0 before it => 010:00

/* -- DO NOT REMOVE --

  • jQuery TimePicker 1.0 plugin
  • Author: Dionlee Uy
  • Email: [email protected]
  • Date: Mon Mar 3 2013
  • @requires jQuery
  • -- DO NOT REMOVE --
    */

(function ($) {

var AM_PM = ['AM','PM'], MAX_HOUR = 12, MAX_MIN = 59,
	ACCEPTABLE_KEYS = [48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105 /*Num keys*/,40,39,38,37 /*Arrow Keys*/,17,16,9 /*Ctrl,Shift,Tab*/];

var DTimePicker = function(elem, options){
	var that = this;
	this.input = $(elem);
	this.activeField = DTimePicker.HOUR;
	this.picker = null;

	this.hour = $('<input class="tp_hr" type="text" style="width:16px;" maxlength="2">');
	this.min = $('<input class="tp_min" type="text" style="width:16px;" maxlength="2">');
	this.ampm = $('<input class="tp_am_pm" type="text" maxlength="2" style="width:25px;">');

	if(options.time){
		var h = parseInt(options.time.substr(0, 2)), m = parseInt(options.time.substr(3, 2));

		//Set values of hour, minute and AM/PM inputs
		this.ampm.val((parseInt(h) > 12 ? "PM" : "AM"));
		this.min.val(m>=10?m:"0"+m);
		h = (h > 12 ? h-12 : h);
		this.hour.val(h>=10?h:"0"+h);

		//Set value of anchor input
		this.setValue();
	}

	//Attach event listeners to inputs
	this.hour.on('focus', function () {
		$(this).select();
		that.activeField = DTimePicker.HOUR;
	}).on('blur', function (e) {
		var val = parseInt($(this).val());
		if(val > MAX_HOUR) $(this).val(MAX_HOUR);
		if(val < 10 && val > 0) $(this).val("0"+val);
		that.setValue();
	}).on('keydown', function (e) {
		if(ACCEPTABLE_KEYS.indexOf(e.keyCode)<0) return false;
	}).on('keyup', function (e) {
		if (e.keyCode == 40){ //Arrow Down/Up button
			that.spinDown();
		} else if(e.keyCode == 38) {
			that.spinUp();
		}
	});
	this.min.on('focus', function () { 
		$(this).select();
		that.activeField = DTimePicker.MIN; 
	}).on('blur', function (e) {
		var val = parseInt($(this).val());
		if(val > MAX_MIN) $(this).val(MAX_MIN);
		if(val < 10 && val > 0) $(this).val("0"+val);
		that.setValue();
	}).on('keydown', function (e) {
		if(ACCEPTABLE_KEYS.indexOf(e.keyCode)<0) return false;
	}).on('keyup', function (e) {
		if (e.keyCode == 40){ //Arrow Down/Up button
			that.spinDown();
		} else if(e.keyCode == 38) {
			that.spinUp();
		}
	});
	this.ampm.on('keydown', function (e) {
		if(e.keyCode == 80) { //P button
			$(this).val("PM").select();
			that.setValue();
		} else if (e.keyCode == 65){ //A button
			$(this).val("AM").select();
			that.setValue();
		} else if (e.keyCode == 40 || e.keyCode == 38){ //Arrow Down/Up button
			that.activeField = DTimePicker.AMPM;
			that.adjustTime(null);
		} else if(e.keyCode == 16 || e.keyCode == 9) { return true; }
		return false;
	}).on('focus', function () { $(this).select(); that.activeField = DTimePicker.AMPM; });

	this.spinnerDiv = $('<div class="tp_spinners"></div>');
	this.spinUpBtn = $('<span class="tp_spinup"></span>');
	this.spinDownBtn = $('<span class="tp_spindown"></span>');

	//Attache event listeners to spinners
	this.spinUpBtn.on('click', function (e) { that.spinUp(); });
	this.spinDownBtn.on('click', function (e) { that.spinDown(); });

	this.create();
}

DTimePicker.prototype = {

	constructor : DTimePicker, 

	create : function(){
		var that = this;
		that.input.wrap('<div class="tp"></div>');
		that.input.hide();

		that.picker = that.input.parent();

		that.picker.append(that.hour).append(":").append(that.min).append(" ").append(that.ampm).append(" ");

		that.spinnerDiv.append(that.spinUpBtn).append(that.spinDownBtn).appendTo(that.picker);
	},

	spinUp : function () { this.adjustTime('p'); },

	spinDown : function () { this.adjustTime('m'); },

	adjustTime : function (op) {
		var that = this;
		switch(this.activeField){
			case DTimePicker.HOUR:
				var val = parseInt(that.hour.val()),
					limit = (op=='p'? MAX_HOUR : 1);
					val = (op=='p'? val+1 : val-1);

				if((op=='p'?(val<=limit):(val>=limit))) { that.hour.val((val<10?"0"+val:val)).select(); }
			break;
			case DTimePicker.MIN:
				var val = parseInt(that.min.val()),
					limit = (op=='p'? MAX_MIN : 0);
					val = (op=='p'? val+1 : val-1);

				if((op=='p'?(val<=limit):(val>=limit))) { that.min.val((val<10?"0"+val:val)).select(); }
			break;
			case DTimePicker.AMPM:
				var val = that.ampm.val();
				that.ampm.val((val=='AM'? "PM" : "AM")).select();
			break;
		}
		this.setValue();
	},

	setValue : function () {
		var ap = this.ampm.val(), h = this.hour.val(), m = this.min.val();
		if(ap=="PM") h=parseInt(h)+12;
		var val = h+":"+m+":00.000";
		this.input.attr('value', val);
	}
}

DTimePicker.HOUR = 1;
DTimePicker.MIN = 2;
DTimePicker.AMPM = 3;

/* DEFINITION FOR TIME PICKER */
$.fn.timepicker = function(opts){
	return $(this).each(function(){
		var $this = $(this),
			data = $(this).data('dtimepicker'),
			options = $.extend({}, $.fn.timepicker.defaults, $this.data(), typeof opts == 'object' && opts);
		if(!data){
			$this.data('dtimepicker', (data = new DTimePicker(this, options)));
		}
		if(typeof opts == 'string') data[opts]();
	});
}

$.fn.timepicker.defaults = {
	time: '12:00:00.000',
}

$.fn.timepicker.Constructor = DTimePicker;

})(jQuery);
Here's the fixed version

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.