Skip to content

Commit 1ae4ee2

Browse files
committed
Handle DateTimes better
1 parent 84c07e3 commit 1ae4ee2

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

Mvc.JQuery.Datatables.Example/Controllers/HomeController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private static List<User> Users()
6565
Position = positions[i%positions.Count],
6666
IsAdmin = i % 11 == 0,
6767
Number = (Numbers) r.Next(4),
68-
Hired = DateTimeOffset.UtcNow.AddDays(-1 * 365 * 3 * r.NextDouble())
68+
Hired = DateTime.UtcNow.AddDays(-1 * 365 * 3 * r.NextDouble())
6969
})
7070
));
7171
}
@@ -88,7 +88,7 @@ public class User
8888

8989
public HomeController.PositionTypes? Position { get; set; }
9090

91-
public DateTimeOffset Hired { get; set; }
91+
public DateTime Hired { get; set; }
9292

9393
public Numbers Number { get; set; }
9494

@@ -105,7 +105,7 @@ public class UserView
105105
public string Email { get; set; }
106106
public bool IsAdmin { get; set; }
107107
public string Position { get; set; }
108-
public DateTimeOffset Hired { get; set; }
108+
public DateTime Hired { get; set; }
109109

110110
public Numbers Number { get; set; }
111111
}

Mvc.JQuery.Datatables/DataTablesFilter.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ public IQueryable FilterPagingSortingSearch(DataTablesParam dtParameters, IQuery
6262

6363
static readonly List<ReturnedFilteredQueryForType> Filters = new List<ReturnedFilteredQueryForType>()
6464
{
65-
Guard(IsDateType, TypeFilters.DateFilter),
65+
66+
Guard(IsDateTimeType, TypeFilters.DateTimeFilter),
67+
Guard(IsDateTimeOffsetType, TypeFilters.DateTimeOffsetFilter),
6668
Guard(IsNumericType, TypeFilters.NumericFilter),
6769
Guard(arg => arg == typeof (string), TypeFilters.StringFilter),
6870
};
@@ -133,9 +135,13 @@ public static bool IsNumericType(Type type)
133135
return false;
134136

135137
}
136-
public static bool IsDateType(Type type)
138+
public static bool IsDateTimeType(Type type)
137139
{
138-
return type == typeof(DateTime) || type == typeof(DateTimeOffset) || type == typeof(DateTime?) || type == typeof(DateTimeOffset?);
140+
return type == typeof(DateTime) || type == typeof(DateTime?) ;
141+
}
142+
public static bool IsDateTimeOffsetType(Type type)
143+
{
144+
return type == typeof(DateTimeOffset) || type == typeof(DateTimeOffset?);
139145
}
140146

141147
}

Mvc.JQuery.Datatables/DynamicLinq/DynamicLinq.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,12 @@ interface IRelationalSignatures : IArithmeticSignatures
676676
void F(string x, string y);
677677
void F(char x, char y);
678678
void F(DateTime x, DateTime y);
679+
void F(DateTimeOffset x, DateTimeOffset y);
680+
679681
void F(TimeSpan x, TimeSpan y);
680682
void F(char? x, char? y);
681683
void F(DateTime? x, DateTime? y);
684+
void F(DateTimeOffset? x, DateTimeOffset? y);
682685
void F(TimeSpan? x, TimeSpan? y);
683686
}
684687

Mvc.JQuery.Datatables/TypeFilters.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static string NumericFilter(string query, string columnname, Type columnT
5858
}
5959
}
6060

61-
public static string DateFilter(string query, string columnname, Type columnType, List<object> parametersForLinqQuery)
61+
public static string DateTimeOffsetFilter(string query, string columnname, Type columnType, List<object> parametersForLinqQuery)
6262
{
6363
if (query.Contains("~"))
6464
{
@@ -69,7 +69,25 @@ public static string DateFilter(string query, string columnname, Type columnType
6969

7070
parametersForLinqQuery.Add(start);
7171
parametersForLinqQuery.Add(end);
72-
return string.Format("{0}.Ticks >= @{1}.Ticks and {0}.Ticks <= @{2}.Ticks", columnname, parametersForLinqQuery.Count - 2, parametersForLinqQuery.Count - 1);
72+
return string.Format("{0} >= @{1} and {0} <= @{2}", columnname, parametersForLinqQuery.Count - 2, parametersForLinqQuery.Count - 1);
73+
}
74+
else
75+
{
76+
return string.Format("{1}.ToLocalTime().ToString(\"g\").{0}", FilterMethod(query), columnname);
77+
}
78+
}
79+
public static string DateTimeFilter(string query, string columnname, Type columnType, List<object> parametersForLinqQuery)
80+
{
81+
if (query.Contains("~"))
82+
{
83+
var parts = query.Split('~');
84+
DateTime start, end;
85+
DateTime.TryParse(parts[0] ?? "", out start);
86+
if (!DateTime.TryParse(parts[1] ?? "", out end)) end = DateTime.MaxValue;
87+
88+
parametersForLinqQuery.Add(start);
89+
parametersForLinqQuery.Add(end);
90+
return string.Format("{0} >= @{1} and {0} <= @{2}", columnname, parametersForLinqQuery.Count - 2, parametersForLinqQuery.Count - 1);
7391
}
7492
else
7593
{

0 commit comments

Comments
 (0)