C# 检查日期是这个日期还是更大

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/978470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:44:08  来源:igfitidea点击:

Check if date is this date or bigger

c#linqdatetime

提问by

I trying to check if a date is this date or bigger inside my linq query with boolean. But it's not working like i want.

我试图用布尔值检查我的 linq 查询中的日期是这个日期还是更大的日期。但它不像我想要的那样工作。

This my code

这是我的代码

        public bool CheckMonth(int month)
    {
            if (month > System.DateTime.Now.Month)
            {
                return true;
            }
            else if (month == System.DateTime.Now.Month)
            {
                return true;
            }
            else
            {
                return false;
            }
    }

    public virtual IList<DateItem> GetThreeDateToList()
    {
        var data = new ScoutDataDataContext();

        var q = (from d in data.DateDetails
                 where d.Activate == 1 && CheckMonth(d.EndDate.Month) 
                 orderby d.Date.Date.Month descending
                 select new DateItem
                 {
                     Title = d.Title,
                     Date = d.Date.Date + " - " + d.EndDate.Date,
                     Link = d.Link,
                 }).Take(3);

        return q.ToList();
    }

Anyone who nows a diffrent way?

现在有不同方式的人吗?

采纳答案by Fredrik M?rk

What is it that you want to do? According to your text you want to find out whether a given date is today or later, but the code sample compares only the month (which means that June this year is the same as June last year). If you want to compare the date (including year and day), this comparison will do the job for you:

你想做什么?根据您的文本,您想找出给定日期是今天还是之后,但代码示例仅比较月份(这意味着今年六月与去年六月相同)。如果您想比较日期(包括年和日),此比较将为您完成这项工作:

yourDate.Date >= DateTime.Now.Date

回答by Marc Gravell

What is the provider here? LINQ-to-SQL? Objects? Entity Framework? (from the name, it looks like LINQ-to-SQL...)

这里的提供者是什么?LINQ 到 SQL?对象?实体框架?(从名字上看,好像是LINQ-to-SQL...)

Indeed, database providers won't be able to process your method; you have a few options:

事实上,数据库提供者将无法处理您的方法;你有几个选择:

  • inline it: && d.EndDate.Month >= System.DateTime.Now.Month
  • map it to a UDF (LINQ-to-SQL only) that accepts a DateTime
  • 内联它: && d.EndDate.Month >= System.DateTime.Now.Month
  • 将其映射到接受一个 UDF(仅限 LINQ-to-SQL) DateTime

The first is probably easier... if you get problems with it not recognising System.DateTime.Now.Month, then do that first into a variable.

第一个可能更容易...如果您遇到无法识别 System.DateTime.Now.Month 的问题,请先将其放入变量中。

回答by Talljoe

This is only checking that the month is greater than the current month, so July 4th, 1776 would pass. I assume you want to check the entire date? Do this:

这只是检查月份是否大于当前月份,因此 1776 年 7 月 4 日将过去。我假设您想检查整个日期?做这个:

    var q = (from d in data.DateDetails
             where d.Activate == 1 && d.EndDate > DateTime.Now) 
             orderby d.Date.Date.Month descending
             select new DateItem
             {
                 Title = d.Title,
                 Date = d.Date.Date + " - " + d.EndDate.Date,
                 Link = d.Link,
             }).Take(3);

A couple of other points:

其他几点:

        if (month < System.DateTime.Now.Month)
        {
            return true;
        }
        else if (month == System.DateTime.Now.Month)
        {
            return true;
        }
        else
        {
            return false;
        }

Is the same as: return month >= System.DateTime.Now;

等同于:返回月份 >= System.DateTime.Now;

I should say almost the same, since you are unecessarily calling DateTime.Now twice, and you might end up with separate values. Call DateTime.Now once and place it in a variable to ensure that you are consistently checking the same time.

我应该说几乎相同,因为您不必要地调用 DateTime.Now 两次,最终可能会得到不同的值。调用 DateTime.Now 一次并将其放入一个变量中,以确保您始终检查相同的时间。

回答by Willy Kimura

Use this method to compare two Dates against each other. It works pretty much the same as DateTime.Comparebut for Date strings.

使用此方法将两个日期相互比较。它的工作原理与DateTime.Compare日期字符串几乎相同。

    /// <summary>
    /// This lets you compare two dates (dd/MM/yyyy) - the first against the second - 
    /// returning an integer specifying the relation between the two.
    /// If 1, then the first date is greater than the second; 
    /// if 0, then the first date is equal to the second; 
    /// if -1, then the first date is less than the second.
    /// </summary>
    /// <param name="Date1">The first date in string format: dd/MM/yyyy</param>
    /// <param name="Date2">The second date in string format: dd/MM/yyyy</param>
    /// <returns>
    /// 1 : The first date is greater than the second. 
    /// 0 : The first date is equal to the second. 
    /// -1 : The first date is less than the second.
    /// </returns>
    public int CompareDates(string Date1, string Date2)
    {
        try
        {
            string separator_value = "/";

            string[] date1_parts = Date1.Split(separator_value.ToCharArray());
            string[] date2_parts = Date2.Split(separator_value.ToCharArray());

            // 0 : Day
            // 1 : Month
            // 2 : Year

            if (string.CompareOrdinal(date1_parts[2], date2_parts[2]) > 0)
            {
                return 1;
            }
            else if (date1_parts[2] == date2_parts[2])
            {
                if (string.CompareOrdinal(date1_parts[1], date2_parts[1]) > 0)
                {
                    return 1;
                }
                else if (date1_parts[1] == date2_parts[1])
                {
                    if (string.CompareOrdinal(date1_parts[0], date2_parts[0]) > 0)
                    {
                        return 1;
                    }
                    else if (date1_parts[0] == date2_parts[0])
                    {
                        return 0;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                return -1;
            }
        }
        catch (Exception)
        {
            return -1;
        }
    }

Here are three examples showing the three forms of validation in action:

以下是三个示例,显示了三种验证形式:

// Is Date-1 greater than Date-2?
if (CompareDates("17/07/2019", "14/07/2019") > 0) // Returns true.
    return true;
else
    return false;

// Is Date-1 less than Date-2?
if (CompareDates("17/07/2019", "14/07/2019") < 0) // Returns false.
    return true;
else
    return false;

// Is Date-1 equal to Date-2?
if (CompareDates("17/02/2019", "14/07/2019") == 0) // Returns false.
    return true;
else
    return false;