Skip to content

Commit

Permalink
Merge pull request #128 from NLilley/date-handling
Browse files Browse the repository at this point in the history
Improve date handling
  • Loading branch information
ithielnor authored Oct 31, 2024
2 parents f031b22 + c23ceee commit 2c2cfa0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 61 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ _ReSharper*/
packages/
.vs
#/.nuget/NuGet.exe

# JetBrains Rider exclusion
/.idea
6 changes: 3 additions & 3 deletions Build/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// Minor Version
// Build Number
// Revision
[assembly: AssemblyVersion("3.7.8")]
[assembly: AssemblyFileVersion("3.7.8")]
[assembly: AssemblyVersion("3.7.9")]
[assembly: AssemblyFileVersion("3.7.9")]
// Uncomment the informational version to create a pre-release package
//[assembly: AssemblyInformationalVersion("3.7.8-beta")]
//[assembly: AssemblyInformationalVersion("3.7.9-beta")]
65 changes: 36 additions & 29 deletions Griddly.NetCore.Razor/wwwroot/js/griddly.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

return val;
case "Date":
return String(val).replace(/[^0-9a-zA-Z-\/]/g, "");
return $.fn.griddly.defaults.getCleanedDate(val);
default:
return val;
}
Expand Down Expand Up @@ -122,12 +122,7 @@

return val;
case "Date":
val = parseForValidDate(val);

if (val == null || !isFinite(val))
return null;
else
return (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear();
return $.fn.griddly.defaults.getFormattedDate(val);
default:
return val;
}
Expand Down Expand Up @@ -315,7 +310,6 @@
// http://stackoverflow.com/q/8098202
var parseForValidDate = function (text)
{

var date = Date.parse(text);

if (isNaN(date))
Expand Down Expand Up @@ -347,26 +341,7 @@
switch (datatype)
{
case "Date":
var date;
var pos;

if (typeof (value) === "string" && (pos = value.indexOf("T")) != -1)
{
value = value.substr(0, pos);

// Strip time, we only want date
var parts = value.split('-');

// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
date = new Date(parts[0], parts[1] - 1, parts[2]); // Note: months are 0-based
}
else
date = new Date(value);

date.setHours(0, 0, 0, 0);

value = date.toLocaleDateString();

value = $.fn.griddly.defaults.getFilterDate(value);
break;
case "Currency":
value = parseFloat(value).toFixed(2);
Expand Down Expand Up @@ -2044,6 +2019,35 @@
return this;
};

var defaultCleanedDate = function(str) {return String(str).replace(/[^0-9a-zA-Z-\/]/g, "");};
var defaultFormatedDate = function(str) {
var val = parseForValidDate(str);
return (val == null || !isFinite(val))
? null
: (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear();
};
var defaultFilterDate = function(strOrDate) {
var date;
var pos;

if (typeof (strOrDate) === "string" && (pos = strOrDate.indexOf("T")) != -1)
{
strOrDate = strOrDate.substr(0, pos);

// Strip time, we only want date
var parts = strOrDate.split('-');

// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
date = new Date(parts[0], parts[1] - 1, parts[2]); // Note: months are 0-based
}
else
date = new Date(strOrDate);

date.setHours(0, 0, 0, 0);

return date.toLocaleDateString();
};

$.fn.griddly.defaults = $.extend({},
{
pageNumber: 0,
Expand All @@ -2065,7 +2069,10 @@
serializeSkipEmpty: true,
filtersSelector: "input[name], select[name]",
exportCustomFunction: null,
exportFunction: null
exportFunction: null,
getCleanedDate: defaultCleanedDate,
getFormattedDate: defaultFormatedDate,
getFilterDate: defaultFilterDate
}, $.fn.griddlyGlobalDefaults);

var GriddlyFilterBar = function (element, options)
Expand Down
65 changes: 36 additions & 29 deletions Griddly/Scripts/griddly.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

return val;
case "Date":
return String(val).replace(/[^0-9a-zA-Z-\/]/g, "");
return $.fn.griddly.defaults.getCleanedDate(val);
default:
return val;
}
Expand Down Expand Up @@ -122,12 +122,7 @@

return val;
case "Date":
val = parseForValidDate(val);

if (val == null || !isFinite(val))
return null;
else
return (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear();
return $.fn.griddly.defaults.getFormattedDate(val);
default:
return val;
}
Expand Down Expand Up @@ -315,7 +310,6 @@
// http://stackoverflow.com/q/8098202
var parseForValidDate = function (text)
{

var date = Date.parse(text);

if (isNaN(date))
Expand Down Expand Up @@ -347,26 +341,7 @@
switch (datatype)
{
case "Date":
var date;
var pos;

if (typeof (value) === "string" && (pos = value.indexOf("T")) != -1)
{
value = value.substr(0, pos);

// Strip time, we only want date
var parts = value.split('-');

// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
date = new Date(parts[0], parts[1] - 1, parts[2]); // Note: months are 0-based
}
else
date = new Date(value);

date.setHours(0, 0, 0, 0);

value = date.toLocaleDateString();

value = $.fn.griddly.defaults.getFilterDate(value);
break;
case "Currency":
value = parseFloat(value).toFixed(2);
Expand Down Expand Up @@ -2044,6 +2019,35 @@
return this;
};

var defaultCleanedDate = function(str) {return String(str).replace(/[^0-9a-zA-Z-\/]/g, "");};
var defaultFormatedDate = function(str) {
var val = parseForValidDate(str);
return (val == null || !isFinite(val))
? null
: (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear();
};
var defaultFilterDate = function(strOrDate) {
var date;
var pos;

if (typeof (strOrDate) === "string" && (pos = strOrDate.indexOf("T")) != -1)
{
strOrDate = strOrDate.substr(0, pos);

// Strip time, we only want date
var parts = strOrDate.split('-');

// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
date = new Date(parts[0], parts[1] - 1, parts[2]); // Note: months are 0-based
}
else
date = new Date(strOrDate);

date.setHours(0, 0, 0, 0);

return date.toLocaleDateString();
};

$.fn.griddly.defaults = $.extend({},
{
pageNumber: 0,
Expand All @@ -2065,7 +2069,10 @@
serializeSkipEmpty: true,
filtersSelector: "input[name], select[name]",
exportCustomFunction: null,
exportFunction: null
exportFunction: null,
getCleanedDate: defaultCleanedDate,
getFormattedDate: defaultFormatedDate,
getFilterDate: defaultFilterDate
}, $.fn.griddlyGlobalDefaults);

var GriddlyFilterBar = function (element, options)
Expand Down

0 comments on commit 2c2cfa0

Please sign in to comment.