简体   繁体   English

如何比较不同的日期值

[英]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. 我需要将其与现在进行比较,如果它已超过1天,请执行某些操作,否则请执行某些操作。

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. 如果这是DateTime值,则可以使用.AddDays()方法并与<>运算符进行比较。

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值,则可以使用自定义日期和时间格式,并与<>运算符进行比较。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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