C# 分析时间、日期/时间或日期

C# 分析时间、日期/时间或日期,c#,parsing,date,time,user-input,C#,Parsing,Date,Time,User Input,我想解析一个包含时间、日期和时间或仅包含日期的输入字符串,我需要知道输入字符串中包含哪些部分 解析实际值不是问题: var dt1 = DateTime.Parse("10:00:00", CultureInfo.CurrentCulture); var dt2 = DateTime.Parse("10pm", CultureInfo.CurrentCulture); var dt3 = DateTime.Parse("01/02/2014", CultureInfo.CurrentCultur

我想解析一个包含时间、日期和时间或仅包含日期的输入字符串,我需要知道输入字符串中包含哪些部分

解析实际值不是问题:

var dt1 = DateTime.Parse("10:00:00", CultureInfo.CurrentCulture);
var dt2 = DateTime.Parse("10pm", CultureInfo.CurrentCulture);
var dt3 = DateTime.Parse("01/02/2014", CultureInfo.CurrentCulture);
var dt4 = DateTime.Parse("01/02/2014 10:00:00", CultureInfo.CurrentCulture);
正如您所期望的,这些都成功地解析了

但是,如果没有提供date元素,DateTime.Parse会自动将当前日期添加到时间中。因此,如果今天是2014年1月1日,那么
DateTime.Parse(“10pm”)
实际上会返回一个设置为2014年1月1日10:00:00的DateTime对象

另外,如果我分析一个没有时间元素的输入字符串,那么DateTime.parse假设时间为00:00:00

因此,在解析完成后,仅通过检查结果DateTime对象,我无法确定原始输入字符串是指定了日期和时间,还是仅指定了日期或时间

我可以使用一个简单的正则表达式来查找标准时间模式,例如
^\d\d:\d\d$
,但我不想假设所有区域性都使用相同的模式来指定时间


如何在不考虑区域性的情况下可靠地检测原始输入字符串中提供了哪些日期和时间元素,而不使用可能仅适用于某些区域性的模糊正则表达式?

您可以使用
DateTime.Parse
datetimestyle.NoCurrentDateDefault
来阻止当前日期:

CultureInfo culture = CultureInfo.InvariantCulture;

var dt1 = DateTime.Parse("10:00:00", culture, DateTimeStyles.NoCurrentDateDefault);
var dt2 = DateTime.Parse("10pm", culture, DateTimeStyles.NoCurrentDateDefault);
var dt3 = DateTime.Parse("01/02/2014", culture, DateTimeStyles.NoCurrentDateDefault);
var dt4 = DateTime.Parse("01/02/2014 10:00:00", culture, DateTimeStyles.NoCurrentDateDefault);
// problem, is this a date only or a date+time?
var dt5 = DateTime.Parse("01/02/2014 00:00:00", culture, DateTimeStyles.NoCurrentDateDefault);
现在是一年。通过这种方式,您至少可以识别时间。您仍然存在区分无时间日期和午夜日期时间的问题

因此,这可能就足够了:

bool dt1TimeOnly, dt1DateOnly, dt1DateAndTime;
dt1TimeOnly = dt1.Year == 1;
dt1DateOnly = !dt1TimeOnly && dt1.TimeOfDay == TimeSpan.FromHours(0);
dt1DateAndTime = !dt1TimeOnly && !dt1DateOnly;
因此,准确识别输入的唯一方法是提供所有支持的格式,并在每个格式上使用
DateTime.TryParseExact

例如,使用此枚举:

public enum DateTimeType
{ 
    Date,
    Time,
    DateTime,
    Unknown
}
这种方法:

public DateTimeType GetDateTimeType(string input, CultureInfo culture, out DateTime parsedDate)
{ 
   if(culture == null) culture = CultureInfo.CurrentCulture;
   var supportedFormats = new[] { 
        new{ Pattern = culture.DateTimeFormat.ShortDatePattern, Type = DateTimeType.Date },
        new{ Pattern = culture.DateTimeFormat.ShortTimePattern, Type = DateTimeType.Time },
        new{ Pattern = culture.DateTimeFormat.LongDatePattern, Type  = DateTimeType.Date },
        new{ Pattern = culture.DateTimeFormat.LongTimePattern, Type  = DateTimeType.Time },
        new{ Pattern = "hhtt", Type = DateTimeType.Time},
        new{ 
            Pattern = culture.DateTimeFormat.ShortDatePattern + " " + culture.DateTimeFormat.LongTimePattern, 
            Type = DateTimeType.DateTime
        }
    };

    foreach(var fi in supportedFormats)
    {
        DateTime dt;
        if (DateTime.TryParseExact(input, fi.Pattern, culture, DateTimeStyles.NoCurrentDateDefault, out dt))
        {
            parsedDate = dt;
            return fi.Type;
        }
    }
    parsedDate = default(DateTime);
    return DateTimeType.Unknown;
}
现在,这将生成正确的日期和日期时间类型:

DateTime dt1;
DateTimeType type1 = GetDateTimeType("10:00:00", culture, out dt1);
DateTime dt2;
DateTimeType type2 = GetDateTimeType("10pm", culture, out dt2);
DateTime dt3;
DateTimeType type3 = GetDateTimeType("01/02/2014", culture, out dt3);
DateTime dt4;
DateTimeType type4 = GetDateTimeType("01/02/2014 10:00:00", culture, out dt4);
DateTime dt5;
DateTimeType type5 = GetDateTimeType("01/02/2014 00:00:00", culture, out dt5);

好吧,它的名字叫
DateTime
我还能用什么来做这个呢?你怎么决定你应该用日期还是时间?有一个很好的图书馆,名字叫from,但我真的不知道它是否能解决你的问题。只是一个进一步研究的提示你想允许任何有效的格式,还是只允许某些特定的格式?这是完美的。。。我没有发现DateTimeStyles.NoCurrentDateDefault选项,这正是我检测纯时间输入所需要的。我想我也可以在午夜约会的时候逃脱。。。谢谢你的帮助@BG100:注意,我添加了一个示例,说明如何解决
日期
时间
问题。