解析时间,日期/时间或日期

时间:2014-01-10 14:46:24

标签: 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.CurrentCulture);
var dt4 = DateTime.Parse("01/02/2014 10:00:00", CultureInfo.CurrentCulture);

这些都可以按照您的预期成功解析。

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

另外,如果我解析没有时间元素的输入字符串,则DateTime.Parse假定时间为00:00:00。

因此,在解析发生后,只需检查生成的DateTime对象,我无法确定原始输入字符串是指定日期和时间,还是仅指定日期或仅指定时间。

我可以使用简单的RegEx来查找标准时间模式,例如^\d\d:\d\d$但我不想假设所有文化都使用相同的模式来指定时间。

如何在不使用文化的情况下,强健地检测原始输入字符串中提供的日期和时间元素,并且不使用某些可能仅适用于某些文化的模糊正则表达式?

1 个答案:

答案 0 :(得分:5)

您可以DateTime.Parse使用DateTimeStyles.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);

现在年份是1.通过这种方式,您至少可以确定时间。你还有问题要区分没有时间和午夜时间的日期。

所以这可能就足够了:

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;
}

现在生成正确的日期和DateTimeTypes

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);
相关问题