Skip to content

Commit

Permalink
fix(date): Manage any date format provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikiki committed Feb 11, 2018
1 parent abdbb5e commit 8648c15
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 9 deletions.
62 changes: 59 additions & 3 deletions dist/bulma-calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ class datePicker {
this.lang = typeof datepicker_langs[this.lang] !== 'undefined' ? this.lang : 'en';
// Set the startDate to the input value
if (this.element.value) {
this.options.startDate = new Date(this.element.value);
this.options.startDate = this._parseDate(this.element.value);
}
// Transform date format according to dateFormat option
this.options.startDate = new Date(this._getFormatedDate(this.options.startDate, this.options.dateFormat));
this.options.startDate = this._parseDate(this._getFormatedDate(this.options.startDate, this.options.dateFormat));
this.month = this.options.startDate.getMonth();
this.year = this.options.startDate.getFullYear();
this.day = this.options.startDate.getDate();
Expand Down Expand Up @@ -445,7 +445,7 @@ class datePicker {
show() {
// Set the startDate to the input value
if (this.element.value) {
this.options.startDate = new Date(this.element.value);
this.options.startDate = this._parseDate(this.element.value);
}
this.month = this.options.startDate.getMonth();
this.year = this.options.startDate.getFullYear();
Expand Down Expand Up @@ -576,6 +576,62 @@ class datePicker {
});
}

_parseDate(dateString, format = undefined) {
const date = new Date();
date.setHours(0, 0, 0, 0);

if (!format) {
format = this.options.dateFormat;
}

const formatPattern = /((?:mm?)|(?:dd?)|(?:yyy?y?))[^0-9]((?:mm?)|(?:dd?)|(?:yyy?y?))[^0-9]((?:mm?)|(?:dd?)|(?:yyy?y?))/i;
const datePattern = /(\d+)[^0-9](\d+)[^0-9](\d+)/i;

let matchFormat = formatPattern.exec(format);
if (matchFormat) {
let matchDate = datePattern.exec(dateString);
if (matchDate) {
switch(matchFormat[1][0]) {
case 'd':
date.setDate(matchDate[1]);
break;
case 'm':
date.setMonth(matchDate[1] - 1);
break;
case 'y':
date.setFullYear(matchDate[1]);
break;
}

switch(matchFormat[2][0]) {
case 'd':
date.setDate(matchDate[2]);
break;
case 'm':
date.setMonth(matchDate[2] - 1);
break;
case 'y':
date.setFullYear(matchDate[2]);
break;
}

switch(matchFormat[3][0]) {
case 'd':
date.setDate(matchDate[3]);
break;
case 'm':
date.setMonth(matchDate[3] - 1);
break;
case 'y':
date.setFullYear(matchDate[3]);
break;
}
}
}

return date;
}

/**
* Check if given year is LeapYear or not
* @method _isLeapYear
Expand Down
Loading

0 comments on commit 8648c15

Please sign in to comment.