Solved

Filter on Time in date time type field.

Posted on 2012-04-03
9
325 Views
Last Modified: 2012-06-03
Hi
Nilesh here,
I have a dataset with a date time type filed. I want to filter that date time field. I want to do filter by time not whole date.

Suppose there is a date time like "2012-04-04 10:34:21", then I want to filter on only time like datetimefiled>="10:34:21".

So how can I solve this?
Thanks in advance.
0
Comment
Question by:avacaralife
9 Comments
 
LVL 6

Expert Comment

by:kswathi
ID: 37804626
you can use Dataview for  the dataset

DataView dv = new DataView(yourdataset);
dv.RowFilter="YourDatecolumn >= #yourdate#";

Also you can refer the below link:
http://www.csharp-examples.net/dataview-rowfilter/
0
 

Author Comment

by:avacaralife
ID: 37804632
Thanks dear kswathi,

I do not want to filter on date, I want to filter on only time like dateTimeFiled>="10:30" like that. No need to check date. its need to check only time.
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 37804661
You can use LINQ for it .

Example:
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID",typeof(int)));
            dt.Columns.Add(new DataColumn("Date",typeof(DateTime)));

            DataRow dr = dt.NewRow();
            dr["ID"] = 1;
            dr["Date"] = new DateTime(2012,4,4,10,10,10);
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = 1;
            dr["Date"] = new DateTime(2012,4,4,11,11,11);
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = 1;
            dr["Date"] = new DateTime(2012,4,4,12,12,12);
            dt.Rows.Add(dr);

            DateTime ToCompare= new DateTime(2012,4,4,10,15,30);

            DataRow[] rows = dt.Rows.Cast<DataRow>().Where(x => (int.Parse(((DateTime)x["Date"]).ToString("HH")) * 3600 +
                                                                 int.Parse(((DateTime)x["Date"]).ToString("mm")) * 60 +
                                                                 int.Parse(((DateTime)x["Date"]).ToString("ss")))
                                                                 >= (ToCompare.Hour*3600 + ToCompare.Minute*60+ToCompare.Second)).ToArray();
0
 

Expert Comment

by:ramgunti
ID: 37804666
Dear friend,

  Do u want to filter the data on a specific day on a specific time or what?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:avacaralife
ID: 37804669
Dear Sir,
Yes I want to filter data on specific time.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 37804688
Example I've provided will search all the rows with the time that are after the one I've provided (after 10:15:30). You'll need to convert it to seconds to compare the values. You can't just compare the string "11:11:11" >= "10:15:30"
0
 

Author Comment

by:avacaralife
ID: 37804694
Ok Thanks sir,

Now I am trying it in sql server.
0
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 38041430
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now