简体   繁体   English

用于字符串比较的DateTime.TryParseExact方法

[英]DateTime.TryParseExact method for string comparison

Hey how can you do a string comparison match for a given date, DateTime.TryParseExact seems like the sensible option but I am not sure how to construct the arguement in the below method: 你怎么能在给定的日期做一个字符串比较匹配, DateTime.TryParseExact似乎是明智的选择,但我不知道如何在下面的方法中构建争论:

public List<Dates> DateEqualToThisDate(string dateentered)
{
    List<Dates> date = dates.Where(
        n => string.Equals(n.DateAdded, 
                           dateentered,
                           StringComparison.CurrentCultureIgnoreCase)).ToList();
        return hiredate;
 }

If you know the format of the date/time exactly (ie it never changes, and does not depend on the culture or locale of the user), then you can use DateTime.TryParseExact . 如果您确切地知道日期/时间的格式(即它永远不会更改,并且不依赖于用户的文化或区域设置),那么您可以使用DateTime.TryParseExact

For example: 例如:

DateTime result;
if (DateTime.TryParseExact(
    str,                            // The string you want to parse
    "dd-MM-yyyy",                   // The format of the string you want to parse.
    CultureInfo.InvariantCulture,   // The culture that was used
                                    // to create the date/time notation
    DateTimeStyles.None,            // Extra flags that control what assumptions
                                    // the parser can make, and where whitespace
                                    // may occur that is ignored.
    out result))                    // Where the parsed result is stored.
{
    // Only when the method returns true did the parsing succeed.
    // Therefore it is in an if-statement and at this point
    // 'result' contains a valid DateTime.
}

The format string can be a fully specified custom date/time format (such as dd-MM-yyyy ), or a general format specifier (such as g ). 格式字符串可以是完全指定的自定义日期/时间格式 (例如dd-MM-yyyy ),也可以是通用格式说明符 (例如g )。 For the latter, the culture matters as to how the date is formatted. 对于后者,文化对于如何格式化日期很重要。 For example, in the Netherlands dates are written as 26-07-2012 ( dd-MM-yyyy ) whereas in the US dates are written as 7/26/2012 ( M/d/yyyy ). 例如,在荷兰,日期写为26-07-2012dd-MM-yyyy ),而在美国,日期写为7/26/2012M/d/yyyy )。

However, this all only works when your string str contains only the date you want to parse. 但是,只有在字符串str仅包含要解析的日期时,这一切才有效。 If you have a bigger string with all sorts of unwanted characters around the date, then you'll have to find the date in there first. 如果你有一个更大的字符串,在日期周围有各种不需要的字符,那么你必须先在那里找到日期。 This can be done using a regular expression, which is a whole other topic in itself. 这可以使用正则表达式来完成,正则表达式本身就是一个完整的其他主题。 Some general information about regular expressions (regex) in C# can be found here . 有关C#中正则表达式(regex)的一些一般信息可以在这里找到。 A regular expression reference is here . 正则表达式引用在这里 For example, a date similar to d/M/yyyy can be found using the regex \\d{1,2}\\/\\d{1,2}\\/\\d{4} . 例如,可以使用正则表达式\\d{1,2}\\/\\d{1,2}\\/\\d{4}找到类似于d/M/yyyy的日期。

Another way of doing it is to convert your date from string to DateTime . 另一种方法是将日期从string转换为DateTime If it is possible I would keep DateAdded as DateTime . 如果有可能我会将DateAdded保留为DateTime

Bellow is a code that runs in LINQPad Bellow是一个在LINQPad中运行的代码

public class Dates
{
    public string DateAdded { get; set; }
}

List<Dates> dates = new List<Dates> {new Dates {DateAdded = "7/24/2012"}, new Dates {DateAdded = "7/25/2012"}};

void Main()
{
    DateEqualToThisDate("7/25/2012").Dump();
}

public List<Dates> DateEqualToThisDate(string anything)
{
    var dateToCompare = DateTime.Parse(anything);

    List<Dates> hireDates = dates.Where(n => DateTime.Parse(n.DateAdded) == dateToCompare).ToList();

    return hireDates;
}

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

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