Skip to content

Commit 85c6389

Browse files
committed
Added 2-digit year formatting option
Also adapted datepicker widget so it wont reset the time when changing date.
1 parent 46d4a86 commit 85c6389

File tree

1 file changed

+50
-47
lines changed

1 file changed

+50
-47
lines changed

src/js/bootstrap-datetimepicker.js

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,16 @@
201201
},
202202

203203
fill: function() {
204-
var d = new Date(this.viewDate),
205-
year = d.getFullYear(),
206-
month = d.getMonth(),
207-
currentDate = this.date.valueOf();
204+
var year = this.viewDate.getFullYear();
205+
var month = this.viewDate.getMonth();
206+
var currentDate = new Date(
207+
this.date.getFullYear(), this.date.getMonth(), this.date.getDate(),
208+
0, 0, 0, 0
209+
);
208210
this.picker.find('.datepicker-days th:eq(1)')
209211
.text(dates[this.language].months[month] + ' ' + year);
210-
var prevMonth = new Date(year, month-1, 28,0,0,0,0),
211-
day = DPGlobal.getDaysInMonth(prevMonth.getFullYear(), prevMonth.getMonth());
212+
var prevMonth = new Date(year, month-1, 28,0,0,0,0);
213+
var day = DPGlobal.getDaysInMonth(prevMonth.getFullYear(), prevMonth.getMonth());
212214
prevMonth.setDate(day);
213215
prevMonth.setDate(day - (prevMonth.getDay() - this.weekStart + 7)%7);
214216
var nextMonth = new Date(prevMonth);
@@ -226,7 +228,7 @@
226228
} else if (prevMonth.getMonth() > month) {
227229
clsName += ' new';
228230
}
229-
if (prevMonth.valueOf() === currentDate) {
231+
if (prevMonth.valueOf() === currentDate.valueOf()) {
230232
clsName += ' active';
231233
}
232234
html.push('<td class="day'+clsName+'">'+prevMonth.getDate() + '</td>');
@@ -294,9 +296,11 @@
294296
this.viewDate.setFullYear(year);
295297
}
296298
if (this.viewMode !== 0) {
297-
this.date.setFullYear(this.viewDate.getFullYear());
298-
this.date.setMonth(this.viewDate.getMonth());
299-
this.date.setDate(this.viewDate.getDate());
299+
this.date = new Date(
300+
this.viewDate.getFullYear(), this.viewDate.getMonth(), this.viewDate.getDate(),
301+
this.date.getHours(), this.date.getMinutes(), this.date.getSeconds(),
302+
this.date.getMilliseconds()
303+
);
300304
this.$element.trigger({
301305
type: 'changeDate',
302306
date: this.date,
@@ -317,9 +321,11 @@
317321
month += 1;
318322
}
319323
var year = this.viewDate.getFullYear();
320-
this.date.setFullYear(year);
321-
this.date.setMonth(month);
322-
this.date.setDate(day);
324+
this.date = new Date(
325+
year, month, day,
326+
this.date.getHours(), this.date.getMinutes(), this.date.getSeconds(),
327+
this.date.getMilliseconds()
328+
);
323329
this.viewDate = new Date(year, month, Math.min(28, day),0,0,0,0);
324330
this.fill();
325331
this.set();
@@ -335,6 +341,8 @@
335341
},
336342

337343
keypress: function(e) {
344+
// TODO: Mask the input so the user can only type
345+
// according to the specified format
338346
},
339347

340348
change: function(e) {
@@ -345,7 +353,6 @@
345353
date: this.date,
346354
viewMode: DPGlobal.modes[this.viewMode].clsName
347355
});
348-
} else {
349356
this.set();
350357
}
351358
},
@@ -375,26 +382,41 @@
375382
var methodName, property, rv;
376383
property = dateFormatComponents[match].property
377384
methodName = 'get' + property;
378-
rv = (dateMethods[methodName] || Date.prototype[methodName]).call(d);
385+
rv = d[methodName]();
386+
if (methodName === 'getMonth') rv = rv + 1;
387+
if (methodName === 'getYear') rv = rv + 1900 - 2000;
379388
return padLeft(rv.toString(), match.length, '0');
380389
});
381390
},
382391

383392
parseDate: function(str) {
384-
var match, i, property, methodName, value, rv = new Date(0);
393+
var match, i, property, methodName, value, parsed = {};
385394
if (!(match = this._formatPattern.exec(str)))
386395
return null;
387396
for (i = 1; i < match.length; i++) {
388397
property = this._propertiesByIndex[i];
389398
if (!property)
390399
continue;
391-
methodName = 'set' + property;
392400
value = match[i];
393401
if (/^\d+$/.test(value))
394402
value = parseInt(value, 10);
395-
(dateMethods[methodName] || Date.prototype[methodName]).call(rv, value);
403+
parsed[property] = value;
396404
}
397-
return rv;
405+
return this._finishParsingDate(parsed);
406+
},
407+
408+
_finishParsingDate: function(parsed) {
409+
var year, month, date, hours, minutes, seconds;
410+
year = parsed.FullYear;
411+
if (parsed.Year) year = 2000 + parsed.Year;
412+
if (!year) year = 1970;
413+
if (parsed.Month) month = parsed.Month - 1;
414+
else month = 0;
415+
date = parsed.Date || 1;
416+
hours = parsed.Hours || 0;
417+
minutes = parsed.Minutes || 0;
418+
seconds = parsed.Seconds || 0;
419+
return new Date(year, month, date, hours, minutes, seconds, 0);
398420
},
399421

400422
_compileFormat: function () {
@@ -531,41 +553,22 @@
531553
var dateFormatComponents = {
532554
dd: {property: 'Date', getPattern: function() { return '(0?[1-9]|[1-2][0-9]|3[0-1])\\b';}},
533555
MM: {property: 'Month', getPattern: function() {return '(0?[1-9]|1[0-2])\\b';}},
534-
month: {property: 'MonthByName', getPattern: function(picker) {
535-
var rv = [], values = dates[picker.language].months;
536-
for (var i = 0; i < values.length; i++)
537-
rv.push(escapeRegExp(values[i]));
538-
return '(' + rv.join('|') + ')';
539-
}},
540-
monthShort: {property: 'MonthByShortName', getPattern: function(picker) {
541-
var rv = [], values = dates[picker.language].monthsShort;
542-
for (var i = 0; i < values.length; i++)
543-
rv.push(escapeRegExp(values[i]));
544-
return '(' + rv.join('|') + ')';
545-
}},
546-
yyyy: {property: 'FullYear', getPattern: function() {return '(\\d{2}|\\d{4})\\b';}},
556+
yy: {property: 'Year', getPattern: function() {return '(\\d{2})\\b'}},
557+
yyyy: {property: 'FullYear', getPattern: function() {return '(\\d{4})\\b';}},
547558
hh: {property: 'Hours', getPattern: function() {return '(0?[0-9]|1[0-9]|2[0-3])\\b';}},
548559
mm: {property: 'Minutes', getPattern: function() {return '(0?[0-9]|[1-5][0-9])\\b';}},
549560
ss: {property: 'Seconds', getPattern: function() {return '(0?[0-9]|[1-5][0-9])\\b';}},
550561
ms: {property: 'Milliseconds', getPattern: function() {return '([0-9]{1,3})\\b';}},
551562
};
552563

553-
var dateMethods = {
554-
getMonth: function() {
555-
return this.getMonth() + 1;
556-
},
557-
setMonth: function(m) {
558-
return this.setMonth(m - 1);
559-
}
560-
};
561-
562-
var tmp = [];
563-
for (var k in dateFormatComponents) tmp.push(k);
564-
tmp.push('.');
564+
var keys = [];
565+
for (var k in dateFormatComponents) keys.push(k);
566+
keys[keys.length - 1] += '\\b';
567+
keys.push('.');
565568

566-
var formatComponent = new RegExp(tmp.join('|'));
567-
tmp.pop();
568-
var formatReplacer = new RegExp(tmp.join('|'), 'g');
569+
var formatComponent = new RegExp(keys.join('\\b|'));
570+
keys.pop();
571+
var formatReplacer = new RegExp(keys.join('\\b|'), 'g');
569572

570573
function escapeRegExp(str) {
571574
// http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex

0 commit comments

Comments
 (0)