DateTime comparison without time part?
Asked Answered
M

5

6

I wrote this code. But I want to ignore time, I want to compare only day.

from s in sayac_okumalari
where s.okuma_tarihi == startDate && s.sayac_id == sayac_id
group s by new { date = new DateTime(((DateTime)s.okuma_tarihi).Year, ((DateTime)s.okuma_tarihi).Month, ((DateTime)s.okuma_tarihi).Day, ((DateTime)s.okuma_tarihi).Hour, 1, 1) } into g
select new
{
     okuma_tarihi = g.Key.date,
     T1 = g.Sum(x => x.toplam_kullanim_T1),
     T2 = g.Sum(x => x.toplam_kullanim_T2),
     T3 = g.Sum(x => x.toplam_kullanim_T3)
};

for example:

25.02.1987 == 25.02.1987
Manton answered 24/7, 2012 at 6:50 Comment(1)
Possible duplicate of How to compare only Date without Time in DateTime types in C#?Soutine
H
17

use s.okuma_tarihi.Value.Date == startDate.Date. This should allow you to compare only the Date component.

Update From the discussion in comments looks like the user is using NullableType. Hence updated the solution for NullableType.

Hermie answered 24/7, 2012 at 6:51 Comment(6)
's.okuma_tarihi' does not contain a definition for 'Date'Sulphuric
Looks like from your code snippet, you need to typecast s.okuma_tarihi to DateTimeHermie
s.okuma_tarihi.Value.Date == startDate.Date is working.If edit your solution I will mark it. Thanks.Sulphuric
((DateTime)s.okuma_tarihi).Date use only calling, assigment.Sulphuric
Updated. But, looks like you are using Nullable type. You may need to ensure that Nullable type has value before performing action.Hermie
This is usefull knowledge for me. Thanks a lot.Sulphuric
I
8

Use Date property of DateTime. For ex,

var date= DateTime.Now.Date;
Influence answered 24/7, 2012 at 6:52 Comment(0)
P
2

Because you could convert s.okuma_tarihi to DateTime, I think you could do:

var sonuc = from s in sayac_okumalari
        where (DateTime)s.okuma_tarihi.Date == startDate.Date && s.sayac_id == sayac_id
        group s by new { date = new DateTime(((DateTime)s.okuma_tarihi).Year, ((DateTime)s.okuma_tarihi).Month, ((DateTime)s.okuma_tarihi).Day, ((DateTime)s.okuma_tarihi).Hour, 1, 1) } into g
        select new
        {
             okuma_tarihi = g.Key.date,
             T1 = g.Sum(x => x.toplam_kullanim_T1),
             T2 = g.Sum(x => x.toplam_kullanim_T2),
             T3 = g.Sum(x => x.toplam_kullanim_T3)
        };

Hope it helps.

Pinhead answered 24/7, 2012 at 7:11 Comment(0)
D
0

Try using the Date property on the DateTime Object will be a good a simple solution.

string AdmissionDate = "10/15/2017" // mm/dd/yyyy
string DepartureDate = "10/14/2017" // mm/dd/yyyy

if (Convert.ToDateTime(AdmissionDate).Date > Convert.ToDateTime(DepartureDate).Date)
{
    // Validation: Admission Date can not be greater than Departure Date... 
}
Disconsider answered 3/9, 2017 at 20:10 Comment(0)
P
-1

Try this...

        try
        {
         DateTime Date1= DateTime.ParseExact(txtBox1.Text,"dd/MM/yyyy",null);
         DateTime Date2 = DateTime.ParseExact(txtBox2.Text, "dd/MM/yyyy", null);

          if (Date1 > Date2) 
          {
           //........
          }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
Psychometry answered 11/7, 2017 at 3:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.