简体   繁体   中英

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?

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.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM