tablesorter/js/parsers/parser-date-extract.js

99 lines
2.7 KiB
JavaScript
Raw Normal View History

/*! Parser: Extract out date - updated 10/26/2014 (v2.18.0) */
2014-03-27 03:18:10 +00:00
/*jshint jquery:true */
;(function($){
'use strict';
2014-03-27 03:18:10 +00:00
var regex = {
usLong : /[A-Z]{3,10}\.?\s+\d{1,2},?\s+(?:\d{4})(?:\s+\d{1,2}:\d{2}(?::\d{2})?(?:\s+[AP]M)?)?/i,
mdy : /(\d{1,2}[\/\s]\d{1,2}[\/\s]\d{4}(\s+\d{1,2}:\d{2}(:\d{2})?(\s+[AP]M)?)?)/i,
dmy : /(\d{1,2}[\/\s]\d{1,2}[\/\s]\d{4}(\s+\d{1,2}:\d{2}(:\d{2})?(\s+[AP]M)?)?)/i,
dmyreplace : /(\d{1,2})[\/\s](\d{1,2})[\/\s](\d{4})/,
ymd : /(\d{4}[\/\s]\d{1,2}[\/\s]\d{1,2}(\s+\d{1,2}:\d{2}(:\d{2})?(\s+[AP]M)?)?)/i,
ymdreplace : /(\d{4})[\/\s](\d{1,2})[\/\s](\d{1,2})/
};
/*! extract US Long Date *//* (ignore any other text)
* e.g. 'Sue's Birthday! Jun 26, 2004 7:22 AM (8# 2oz)'
* demo: http://jsfiddle.net/Mottie/abkNM/4165/ */
2014-03-27 03:18:10 +00:00
$.tablesorter.addParser({
id: 'extractUSLongDate',
2014-10-27 00:11:12 +00:00
is: function () {
2014-03-27 03:18:10 +00:00
// don't auto detect this parser
return false;
},
2014-10-27 00:11:12 +00:00
format: function (s) {
var date,
str = s ? s.match(regex.usLong) : s;
if (str) {
date = new Date( str[0] );
return date instanceof Date && isFinite(date) ? date.getTime() : s;
}
return s;
2014-03-27 03:18:10 +00:00
},
type: 'numeric'
2014-03-27 03:18:10 +00:00
});
/*! extract MMDDYYYY *//* (ignore any other text)
* demo: http://jsfiddle.net/Mottie/abkNM/4166/ */
2014-03-27 03:18:10 +00:00
$.tablesorter.addParser({
id: 'extractMMDDYYYY',
2014-10-27 00:11:12 +00:00
is: function () {
2014-03-27 03:18:10 +00:00
// don't auto detect this parser
return false;
},
2014-10-27 00:11:12 +00:00
format: function (s) {
var date,
str = s ? s.replace(/\s+/g, ' ').replace(/[\-.,]/g, '/').match(regex.mdy) : s;
if (str) {
date = new Date( str[0] );
return date instanceof Date && isFinite(date) ? date.getTime() : s;
}
return s;
2014-03-27 03:18:10 +00:00
},
type: 'numeric'
2014-03-27 03:18:10 +00:00
});
/*! extract DDMMYYYY *//* (ignore any other text)
* demo: http://jsfiddle.net/Mottie/abkNM/4167/ */
2014-03-27 03:18:10 +00:00
$.tablesorter.addParser({
id: 'extractDDMMYYYY',
2014-10-27 00:11:12 +00:00
is: function () {
2014-03-27 03:18:10 +00:00
// don't auto detect this parser
return false;
},
2014-10-27 00:11:12 +00:00
format: function (s) {
var date,
str = s ? s.replace(/\s+/g, ' ').replace(/[\-.,]/g, '/').match(regex.dmy) : s;
if (str) {
date = new Date( str[0].replace(regex.dmyreplace, '$2/$1/$3') );
return date instanceof Date && isFinite(date) ? date.getTime() : s;
2014-03-27 03:18:10 +00:00
}
return s;
},
type: 'numeric'
2014-03-27 03:18:10 +00:00
});
/*! extract YYYYMMDD *//* (ignore any other text)
* demo: http://jsfiddle.net/Mottie/abkNM/4168/ */
2014-03-27 03:18:10 +00:00
$.tablesorter.addParser({
id: 'extractYYYYMMDD',
2014-10-27 00:11:12 +00:00
is: function () {
2014-03-27 03:18:10 +00:00
// don't auto detect this parser
return false;
},
2014-10-27 00:11:12 +00:00
format: function (s) {
var date,
str = s ? s.replace(/\s+/g, ' ').replace(/[\-.,]/g, '/').match(regex.ymd) : s;
if (str) {
date = new Date( str[0].replace(regex.ymdreplace, '$2/$3/$1') );
return date instanceof Date && isFinite(date) ? date.getTime() : s;
2014-03-27 03:18:10 +00:00
}
return s;
},
type: 'numeric'
2014-03-27 03:18:10 +00:00
});
})(jQuery);