diff --git a/jquery.date_input.js b/jquery.date_input.js index 84ae311..75cbd47 100644 --- a/jquery.date_input.js +++ b/jquery.date_input.js @@ -133,6 +133,7 @@ DateInput.prototype = { }, show: function() { + this.setShowDate(); this.rootLayers.css("display", "block"); $([window, document.body]).click(this.hideIfClickOutside); this.input.unbind("focus", this.show); @@ -140,6 +141,26 @@ DateInput.prototype = { this.setPosition(); }, + //defalut set Date + setShowDate: function(){ + var _val = this.input.val(); + if(this.isDate(_val)){ + var regex = /^(\d{4})-(\d{2})-(\d{2})$/; + var r = _val.replace(regex, '$1/$2/$3'); + var y = parseInt(RegExp.$1, 10), m = parseInt(RegExp.$2, 10) - 1, d = parseInt(RegExp.$3, 10); + var _Date = new Date(y, m, d); + this.selectDate(_Date); + } + }, + + isEmpty: function(v){ + return ((v == null) || (v.length == 0)); + }, + + isDate: function(v){ + return !this.isEmpty(v) && /^(\d+)-(\d{1,2})-(\d{1,2})$/.test(v); + }, + hide: function() { this.rootLayers.css("display", "none"); $([window, document.body]).unbind("click", this.hideIfClickOutside); @@ -156,7 +177,7 @@ DateInput.prototype = { // Returns true if the given event occurred inside the date selector insideSelector: function(event) { - var offset = this.dateSelector.position(); + var offset = this.dateSelector.offset(); offset.right = offset.left + this.dateSelector.outerWidth(); offset.bottom = offset.top + this.dateSelector.outerHeight(); @@ -212,11 +233,19 @@ DateInput.prototype = { }, dateToString: function(date) { - return date.getDate() + " " + this.short_month_names[date.getMonth()] + " " + date.getFullYear(); + //return date.getDate() + " " + this.short_month_names[date.getMonth()] + " " + date.getFullYear(); + var y = date.getFullYear().toString(), m = (date.getMonth() + 1).toString(), d = date.getDate().toString(); + if(m.length == 1){ + m = "0"+ m; + } + if(d.length == 1){ + d = "0" + d; + } + return y + "-" + m + "-" + d; }, setPosition: function() { - var offset = this.input.offset(); + var offset = this.input.position(); this.rootLayers.css({ top: offset.top + this.input.outerHeight(), left: offset.left diff --git a/translations/jquery.date_input.zh_CN.js b/translations/jquery.date_input.zh_CN.js index 92ff482..a3b4726 100644 --- a/translations/jquery.date_input.zh_CN.js +++ b/translations/jquery.date_input.zh_CN.js @@ -1,5 +1,5 @@ jQuery.extend(DateInput.DEFAULT_OPTS, { - month_names: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"], - short_month_names: ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"], - short_day_names: ["一", "二", "三", "四", "五", "六", "日"] + month_names: ["\u4e00\u6708", "\u4e8c\u6708", "\u4e09\u6708", "\u56db\u6708", "\u4e94\u6708", "\u516d\u6708", "\u4e03\u6708", "\u516b\u6708", "\u4e5d\u6708", "\u5341\u6708", "\u5341\u4e00\u6708", "\u5341\u4e8c\u6708"], + short_month_names: ["\u4e00", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d", "\u4e03", "\u516b", "\u4e5d", "\u5341", "\u5341\u4e00", "\u5341\u4e8c"], + short_day_names: ["\u65e5", "\u4e00", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d"] });