Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the support for ISO 8601 date/time formats #2189

Merged
merged 1 commit into from
Oct 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 78 additions & 22 deletions jquery.jtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,45 @@ THE SOFTWARE.

var displayFormat = field.displayFormat || this.options.defaultDateFormat;
var date = this._parseDate(fieldValue);
return $.datepicker.formatDate(displayFormat, date);
// Gp - if an error occur, return a format error
try {
return this._formatDate(displayFormat, date);
} catch (e) {
return date; //fieldValue;
}
},

/* Format the date/time field.
*************************************************************************/
_formatDate: function (format, date) {

var pad = function (n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
};

format = format.replace('ss', pad(date.getSeconds(), 2));
format = format.replace('s', date.getSeconds());
format = format.replace('dd', pad(date.getDate(), 2));
format = format.replace('d', date.getDate());
format = format.replace('mm', pad(date.getMinutes(), 2));
format = format.replace('m', date.getMinutes());
//format = format.replace('MMMM', monthNames[date.getMonth()]);
//format = format.replace('MMM', monthNames[date.getMonth()].substring(0, 3));
format = format.replace('MM', pad(date.getMonth() + 1, 2));
format = format.replace(/M(?![ao])/, date.getMonth() + 1);
//format = format.replace('DD', Days[date.getDay()]);
//format = format.replace(/D(?!e)/, Days[date.getDay()].substring(0, 3));
format = format.replace('yyyy', date.getFullYear());
format = format.replace('YYYY', date.getFullYear());
format = format.replace('yy', (date.getFullYear() + "").substring(2));
format = format.replace('YY', (date.getFullYear() + "").substring(2));
format = format.replace('HH', pad(date.getHours(), 2));
format = format.replace('H', date.getHours());
format = format.replace('hh', pad(date.getHours(), 2));
format = format.replace('h', date.getHours());
return format;
},

/* Gets options for a field according to user preferences.
Expand Down Expand Up @@ -971,29 +1009,47 @@ THE SOFTWARE.
* /Date(1320259705710)/
* 2011-01-01 20:32:42 (YYYY-MM-DD HH:MM:SS)
* 2011-01-01 (YYYY-MM-DD)
* 2011-10-15T14:42:51 (ISO 8601)
*************************************************************************/
_parseDate: function (dateString) {
if (dateString.indexOf('Date') >= 0) { //Format: /Date(1320259705710)/
return new Date(
parseInt(dateString.substr(6), 10)
);
} else if (dateString.length == 10) { //Format: 2011-01-01
return new Date(
parseInt(dateString.substr(0, 4), 10),
parseInt(dateString.substr(5, 2), 10) - 1,
parseInt(dateString.substr(8, 2), 10)
);
} else if (dateString.length == 19) { //Format: 2011-01-01 20:32:42
return new Date(
parseInt(dateString.substr(0, 4), 10),
parseInt(dateString.substr(5, 2), 10) - 1,
parseInt(dateString.substr(8, 2), 10),
parseInt(dateString.substr(11, 2), 10),
parseInt(dateString.substr(14, 2), 10),
parseInt(dateString.substr(17, 2), 10)
);
} else {
this._logWarn('Given date is not properly formatted: ' + dateString);
try {
if (dateString.indexOf('Date') >= 0) { //Format: /Date(1320259705710)/
return new Date(
parseInt(dateString.substr(6), 10)
);
} else if (dateString.length == 10) { //Format: 2011-01-01
return new Date(
parseInt(dateString.substr(0, 4), 10),
parseInt(dateString.substr(5, 2), 10) - 1,
parseInt(dateString.substr(8, 2), 10)
);
} else if (dateString.length == 19) { //Format: 2011-01-01 20:32:42
return new Date(
parseInt(dateString.substr(0, 4), 10),
parseInt(dateString.substr(5, 2), 10) - 1,
parseInt(dateString.substr(8, 2), 10),
parseInt(dateString.substr(11, 2), 10),
parseInt(dateString.substr(14, 2), 10),
parseInt(dateString.substr(17, 2), 10)
);
// added by Gp
} else if (dateString.indexOf('T') > 0) { //Format: ISO 8601 2009-10-15T14:42:51
var dtstr = dateString.replace(/\D/g, " ");
var dtcomps = dtstr.split(" ");
dtcomps[1]--; // modify month between 1 based ISO 8601 and zero based Date
return new Date(
Date.UTC(
dtcomps[0],
dtcomps[1],
dtcomps[2],
dtcomps[3],
dtcomps[4],
dtcomps[5]));
} else {
throw 'Given date is not properly formatted: ' + dateString;
}
} catch (e) {
this._logWarn(e);
return 'format error!';
}
},
Expand Down