This is a migrated thread and some comments may be shown as answers.

DateTime Filter Problem

5 Answers 96 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 27 Sep 2012, 01:55 PM
I'm trying to use the MonthYearPicker as my control for filtering datetime. However, when I try the code below, I get:

String was not recognized as a valid DateTime.

I've tried formatting several ways with no luck. I was trying to look at the event in the code behind, but I see no arguments at all that contains my filter values themselves. Just the  column name and expression.  Any help would be appreciated. Thank you!










<
FilterTemplate>
                     
                    <telerik:RadMonthYearPicker ID="RadMonthYearPicker1" runat="server" ClientEvents-OnPopupClosing="MeasurementDateChanged" />
 
                            <telerik:RadScriptBlock ID="RadScriptBlock3" runat="server">
 
                                <script type="text/javascript">
                                    function MeasurementDateChanged(sender, args) {
                                        var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                        var year = args._picker.FocusedDate[0];
                                        var month = args._picker.FocusedDate[1];
 
                                        var fromDate = new Date(year, month-1, 1);
                                        var toDate = new Date(year, month, 0);
 
 
                                        tableView.filter("MeasurementDate", fromDate.toISOString() + " " + toDate.toISOString(), "Between");
                                    }
                                </script>
 
                            </telerik:RadScriptBlock>
                    </FilterTemplate>

5 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 29 Sep 2012, 07:21 AM
Hi Michael,

After thoroughly examining you code I have noticed some details that could be changed so you can achieve your goal.
  • I suggest that you use the OnDateSelected event instead of OnPopupClosing
  • We do not recommend the usage of underscore properties as they serve for internal use and I suggest that you extract you date using get_selectedDate() method
  • You also could format your dates in the following way
    fromDate = fromDate.getMonth() + "/" + fromDate.getDate() + "/" + fromDate.getFullYear();
    toDate = toDate.getMonth() + "/" + toDate.getDate() + "/" + toDate.getFullYear();
In the code snippet below is the entire rewritten function that we tested and it works:
<FilterTemplate>
                        <telerik:RadMonthYearPicker ID="RadMonthYearPicker1" runat="server" ClientEvents-OnDateSelected="MeasurementDateChanged" />
                        <script type="text/javascript">
                            function MeasurementDateChanged(sender, args) {
                                var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                var selectedDate = sender.get_selectedDate();
                                var year = selectedDate.getFullYear();
                                var month = selectedDate.getMonth();
 
                                var fromDate = new Date(year, month - 1, 1);
                                var toDate = new Date(year, month, 0);
                                fromDate = fromDate.getMonth() + "/" + fromDate.getDate() + "/" + fromDate.getFullYear();
                                toDate = toDate.getMonth() + "/" + toDate.getDate() + "/" + toDate.getFullYear();
                                tableView.filter("MeasurementDate", fromDate + " " + toDate, "Between");
                            }
                        </script>
                    </FilterTemplate>

For your convenience I have attached a sample project illustrating the filtering.

All the best,
Angel Petrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Michael
Top achievements
Rank 1
answered on 01 Oct 2012, 07:56 PM
I tried this. I got the same exact error. Is there a place I can inspect the value of the string it's trying to convert? I've tried setting breakpoints but I can't seem to find this value...
0
Angel Petrov
Telerik team
answered on 03 Oct 2012, 03:17 PM
Hi Michael,

I think I have found the problem. When you use getMonth() method it returns you a value from the range form 0 to 11 according to the month. But when you call tableView.filter(...) method it expects the values for month to be from 1 to 12. I have modified the javascript function and you can see it in the code snipped:
function MeasurementDateChanged(sender, args) {
                                var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                var selectedDate = sender.get_selectedDate();
                                var year = selectedDate.getFullYear();
                                var month = selectedDate.getMonth();
                                var fromDate;
                                if (month == 0) {
                                    fromDate = new Date(year - 1, 11, 1);
                                } else {
                                    fromDate = new Date(year, month - 1, 1);
                                }
                                var toDate = new Date(year, month, 1);
                                fromDate = fromDate.getMonth() + 1 + "/" + fromDate.getDate() + "/" + fromDate.getFullYear();
                                toDate = toDate.getMonth() + 1 + "/" + toDate.getDate() + "/" + toDate.getFullYear();
                                tableView.filter("MeasurementDate", fromDate + " " + toDate, "Between");
                            }
Give it a try and see how it behaves.

Kind regards,
Angel Petrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Michael
Top achievements
Rank 1
answered on 03 Oct 2012, 03:41 PM
No love... see attached screenshot. Followed up by:

String was not recognized as a valid DateTime.

0
Angel Petrov
Telerik team
answered on 05 Oct 2012, 04:10 PM
Hello Michael,

I have tested the code which I sent you in my previous post as you can see from this video and it is working properly. I am also attaching the sample project that I am using. You could review it and spot any differences in the usage.

Kind regards,
Angel Petrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Michael
Top achievements
Rank 1
Share this question
or