onur
onur

Reputation: 6365

How can I compare different date values

I have date column on my database like this:

201411241440

year month day hour minute

I need to compare it with now and if it's older than 1 day, do something, else do something.

I think, I can do like this on regular date format

DateTime isold = DateTime.Today.AddDays(-1)

And compare.

How can I do this with my date format? And how can I compare date values?

Upvotes: 1

Views: 90

Answers (1)

Soner Gönül
Soner Gönül

Reputation: 98750

If this is a DateTime value, you can use .AddDays() method and compare with < and > operators.

if(yourDateTime < DateTime.UtcNow.AddDays(-1))
{ 
   // do something.
}

If this is a string value, you can use custom date and time formatting and compare with < and > operators.

string s = "201411241440";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyyMMddHHmm",
                          CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dt))
{
    if(dt < DateTime.UtcNow.AddDays(-1))
    { 
        // do something.
    }
}

Upvotes: 9

Related Questions