DateTime.TryParse century control C#

前端 未结 5 1506
轻奢々
轻奢々 2020-11-28 15:57

The result of the following snippet is \"12/06/1930 12:00:00\". How do I control the implied century so that \"12 Jun 30\" becomes 2030 instead?

    string          


        
5条回答
  •  广开言路
    2020-11-28 16:31

    I had a similar problem and I solved it with a Regex. In your case it would look like that:

    private static readonly Regex DateRegex = new Regex(
    @"^[0-9][0-9] (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9][0-9]$",
    RegexOptions.Compiled | RegexOptions.ExplicitCapture);
    
    private static string Beautify(string date)
    {
        var match = DateRegex.Match(date);
        if (match.Success)
        {
            // Maybe further checks for correct day
            return date.Insert("dd-MMM-".Length, "20");
        }
    
        return date;
    }
    

提交回复
热议问题