Skip to content

Commit 2c2cfa0

Browse files
authored
Merge pull request #128 from NLilley/date-handling
Improve date handling
2 parents f031b22 + c23ceee commit 2c2cfa0

File tree

4 files changed

+78
-61
lines changed

4 files changed

+78
-61
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,6 @@ _ReSharper*/
5050
packages/
5151
.vs
5252
#/.nuget/NuGet.exe
53+
54+
# JetBrains Rider exclusion
55+
/.idea

Build/CommonAssemblyInfo.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// Minor Version
1313
// Build Number
1414
// Revision
15-
[assembly: AssemblyVersion("3.7.8")]
16-
[assembly: AssemblyFileVersion("3.7.8")]
15+
[assembly: AssemblyVersion("3.7.9")]
16+
[assembly: AssemblyFileVersion("3.7.9")]
1717
// Uncomment the informational version to create a pre-release package
18-
//[assembly: AssemblyInformationalVersion("3.7.8-beta")]
18+
//[assembly: AssemblyInformationalVersion("3.7.9-beta")]

Griddly.NetCore.Razor/wwwroot/js/griddly.js

+36-29
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
return val;
5252
case "Date":
53-
return String(val).replace(/[^0-9a-zA-Z-\/]/g, "");
53+
return $.fn.griddly.defaults.getCleanedDate(val);
5454
default:
5555
return val;
5656
}
@@ -122,12 +122,7 @@
122122

123123
return val;
124124
case "Date":
125-
val = parseForValidDate(val);
126-
127-
if (val == null || !isFinite(val))
128-
return null;
129-
else
130-
return (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear();
125+
return $.fn.griddly.defaults.getFormattedDate(val);
131126
default:
132127
return val;
133128
}
@@ -315,7 +310,6 @@
315310
// http://stackoverflow.com/q/8098202
316311
var parseForValidDate = function (text)
317312
{
318-
319313
var date = Date.parse(text);
320314

321315
if (isNaN(date))
@@ -347,26 +341,7 @@
347341
switch (datatype)
348342
{
349343
case "Date":
350-
var date;
351-
var pos;
352-
353-
if (typeof (value) === "string" && (pos = value.indexOf("T")) != -1)
354-
{
355-
value = value.substr(0, pos);
356-
357-
// Strip time, we only want date
358-
var parts = value.split('-');
359-
360-
// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
361-
date = new Date(parts[0], parts[1] - 1, parts[2]); // Note: months are 0-based
362-
}
363-
else
364-
date = new Date(value);
365-
366-
date.setHours(0, 0, 0, 0);
367-
368-
value = date.toLocaleDateString();
369-
344+
value = $.fn.griddly.defaults.getFilterDate(value);
370345
break;
371346
case "Currency":
372347
value = parseFloat(value).toFixed(2);
@@ -2044,6 +2019,35 @@
20442019
return this;
20452020
};
20462021

2022+
var defaultCleanedDate = function(str) {return String(str).replace(/[^0-9a-zA-Z-\/]/g, "");};
2023+
var defaultFormatedDate = function(str) {
2024+
var val = parseForValidDate(str);
2025+
return (val == null || !isFinite(val))
2026+
? null
2027+
: (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear();
2028+
};
2029+
var defaultFilterDate = function(strOrDate) {
2030+
var date;
2031+
var pos;
2032+
2033+
if (typeof (strOrDate) === "string" && (pos = strOrDate.indexOf("T")) != -1)
2034+
{
2035+
strOrDate = strOrDate.substr(0, pos);
2036+
2037+
// Strip time, we only want date
2038+
var parts = strOrDate.split('-');
2039+
2040+
// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
2041+
date = new Date(parts[0], parts[1] - 1, parts[2]); // Note: months are 0-based
2042+
}
2043+
else
2044+
date = new Date(strOrDate);
2045+
2046+
date.setHours(0, 0, 0, 0);
2047+
2048+
return date.toLocaleDateString();
2049+
};
2050+
20472051
$.fn.griddly.defaults = $.extend({},
20482052
{
20492053
pageNumber: 0,
@@ -2065,7 +2069,10 @@
20652069
serializeSkipEmpty: true,
20662070
filtersSelector: "input[name], select[name]",
20672071
exportCustomFunction: null,
2068-
exportFunction: null
2072+
exportFunction: null,
2073+
getCleanedDate: defaultCleanedDate,
2074+
getFormattedDate: defaultFormatedDate,
2075+
getFilterDate: defaultFilterDate
20692076
}, $.fn.griddlyGlobalDefaults);
20702077

20712078
var GriddlyFilterBar = function (element, options)

Griddly/Scripts/griddly.js

+36-29
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
return val;
5252
case "Date":
53-
return String(val).replace(/[^0-9a-zA-Z-\/]/g, "");
53+
return $.fn.griddly.defaults.getCleanedDate(val);
5454
default:
5555
return val;
5656
}
@@ -122,12 +122,7 @@
122122

123123
return val;
124124
case "Date":
125-
val = parseForValidDate(val);
126-
127-
if (val == null || !isFinite(val))
128-
return null;
129-
else
130-
return (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear();
125+
return $.fn.griddly.defaults.getFormattedDate(val);
131126
default:
132127
return val;
133128
}
@@ -315,7 +310,6 @@
315310
// http://stackoverflow.com/q/8098202
316311
var parseForValidDate = function (text)
317312
{
318-
319313
var date = Date.parse(text);
320314

321315
if (isNaN(date))
@@ -347,26 +341,7 @@
347341
switch (datatype)
348342
{
349343
case "Date":
350-
var date;
351-
var pos;
352-
353-
if (typeof (value) === "string" && (pos = value.indexOf("T")) != -1)
354-
{
355-
value = value.substr(0, pos);
356-
357-
// Strip time, we only want date
358-
var parts = value.split('-');
359-
360-
// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
361-
date = new Date(parts[0], parts[1] - 1, parts[2]); // Note: months are 0-based
362-
}
363-
else
364-
date = new Date(value);
365-
366-
date.setHours(0, 0, 0, 0);
367-
368-
value = date.toLocaleDateString();
369-
344+
value = $.fn.griddly.defaults.getFilterDate(value);
370345
break;
371346
case "Currency":
372347
value = parseFloat(value).toFixed(2);
@@ -2044,6 +2019,35 @@
20442019
return this;
20452020
};
20462021

2022+
var defaultCleanedDate = function(str) {return String(str).replace(/[^0-9a-zA-Z-\/]/g, "");};
2023+
var defaultFormatedDate = function(str) {
2024+
var val = parseForValidDate(str);
2025+
return (val == null || !isFinite(val))
2026+
? null
2027+
: (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear();
2028+
};
2029+
var defaultFilterDate = function(strOrDate) {
2030+
var date;
2031+
var pos;
2032+
2033+
if (typeof (strOrDate) === "string" && (pos = strOrDate.indexOf("T")) != -1)
2034+
{
2035+
strOrDate = strOrDate.substr(0, pos);
2036+
2037+
// Strip time, we only want date
2038+
var parts = strOrDate.split('-');
2039+
2040+
// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
2041+
date = new Date(parts[0], parts[1] - 1, parts[2]); // Note: months are 0-based
2042+
}
2043+
else
2044+
date = new Date(strOrDate);
2045+
2046+
date.setHours(0, 0, 0, 0);
2047+
2048+
return date.toLocaleDateString();
2049+
};
2050+
20472051
$.fn.griddly.defaults = $.extend({},
20482052
{
20492053
pageNumber: 0,
@@ -2065,7 +2069,10 @@
20652069
serializeSkipEmpty: true,
20662070
filtersSelector: "input[name], select[name]",
20672071
exportCustomFunction: null,
2068-
exportFunction: null
2072+
exportFunction: null,
2073+
getCleanedDate: defaultCleanedDate,
2074+
getFormattedDate: defaultFormatedDate,
2075+
getFilterDate: defaultFilterDate
20692076
}, $.fn.griddlyGlobalDefaults);
20702077

20712078
var GriddlyFilterBar = function (element, options)

0 commit comments

Comments
 (0)