【问题标题】:Parsing Time, Date/Time, or Date解析时间、日期/时间或日期
【发布时间】:2014-01-29 14:19:54
【问题描述】:

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

解析实际值不成问题:

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") 实际上会返回一个设置为 2014 年 1 月 1 日 10:00:00 的 DateTime 对象。

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

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

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

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

【问题讨论】:

  • 嗯,它被命名为DateTime
  • 我还能用什么来做到这一点?
  • 你如何确定应该使用日期还是时间?
  • Jon Skeet 中有一个名为NodaTime 的优秀库,但我真的不知道它是否能解决您的问题。只是进一步研究的提示
  • 您要允许任何有效格式,还是只允许某些特定格式?

标签: c# parsing date time user-input


【解决方案1】:

您可以将DateTime.ParseDateTimeStyles.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);

【讨论】:

  • 这太完美了...我没有发现 DateTimeStyles.NoCurrentDateDefault 选项,这正是我检测仅时间输入所需要的。我想我也可以摆脱仅限日期的午夜部分......所以感谢您的帮助!
  • @BG100:请注意,我添加了一个关于如何解决DateTime 问题的示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 2022-01-18
  • 1970-01-01
  • 2013-02-04
  • 2015-07-22
  • 2020-09-04
  • 1970-01-01
相关资源
最近更新 更多