-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.timeformat.min.js
10 lines (10 loc) · 1.91 KB
/
jquery.timeformat.min.js
1
2
3
4
5
6
7
8
9
10
/* # jQuery.timeformat #
* ## Version 1 ##
* formats a time input on blur event
* this is beta - logic could use a bit of reworking,
* and there is much room for efficiency tweaks.
*/
$(function(){function g(){h=Math.floor(24*Math.random()).toString();i=Math.floor(59*Math.random()).toString();return h+i}var c={blur:"blur",change:"change"},m={"h:ia":/^([0-9]{1,2}):([0-9]{2})\s?([a-zA-Z]{1,2})/,"h:i":/(^[0-9]{1,2}):([0-9]{2})/,hi:/^([0-9]{1,2})([0-9]{2})/,ha:/^([0-9]{1,2})\s?([a-zA-Z]{1,2})/,h:/^([0-9]{1,2})/},n={h:"24",i:"59"},p={h:1,i:0},k={now:function(){return(new Date).format("H:ia")},random:g,whatever:g,dawn:"6:30am",morning:"8:00am",dusk:"5:00pm",noon:"12:00pm",afternoon:"2:00pm",
evening:"6:00pm",night:"8:00pm",late:"11:00pm"},d;d=function(a,e){this.$el=$(a);this.options=e;this._name="timeformat";this.init()};d.prototype={init:function(){for(var a in c)if(c.hasOwnProperty(a))this.$el.on(a,$.proxy(this[c[a]],this))},blur:function(){this.date=this.format()},format:function(){var a=this.$el.val().trim()||"",e={h:"0",i:"00",a:"am"},l=[],j;k[a]&&(a="function"==typeof k[a]?k[a]():k[a]);for(var c in m)if(m.hasOwnProperty(c)){var d=a.match(m[c]);d&&d.length>l.length&&(l=d,j=c)}if(0<
l.length){var a=l,g={"h:ia":["h","i","a"],"h:i":["h","i"],hi:["h","i"],ha:["h","a"],h:["h"]}[j];j=a.slice(1);var f={h:0,i:"00",a:"am"};console.log(g,j,a);$(j).each(function(a,b){console.log(arguments);var c=g[a],d=n[c],e=p[c];switch(c){case "h":case "i":b=parseInt(b);if(isNaN(b))b=e;else if(b>d||b<e)b=b>d?d:e;b="i"==c&&10>b?"0"+b:b.toString();break;case "a":b=~b.search("a")?"am":"pm"}f[c]=b});12<f.h&&(f.a="pm",f.h-=12);f.H="pm"==f.a?f.h+12:f.h;e=f;this.val=e.h+":"+e.i+e.a;this.$el.val(this.val)}else this.$el.val(this.val||
"");this.$el[0].time=e},change:function(){""==this.$el.val()&&(this.val="")}};$.fn.timeformat=function(a){return this.each(function(){$.data(this,"plugin_timeformat")||($.data(this,"plugin_timeformat"),new d(this,a))})}});