Solved

Filter on Time in date time type field.

Posted on 2012-04-03
9
326 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

24 Experts available now in Live!

Get 1:1 Help Now