2014-01-10 130 views
5

我想解析一個包含時間,日期和時間,或者只是一個日期的輸入字符串,並且我需要知道輸入字符串中包含哪些部分。解析時間,日期/時間或日期

解析的實際值是沒有問題的:

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$但我不想假定所有文化都使用相同的模式來指定時間。

我該如何可靠地檢測哪些日期和時間元素在原始輸入字符串中提供,而不管文化如何,以及不使用某些僅適用於某些文化的模糊正則表達式?

+0

那麼,它被命名爲'日期時間' – Steve

+0

我還有什麼可以用來做到這一點? – BG100

+0

你如何確定你應該使用日期還是時間? – scheien

回答

5

你可以使用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); 
+0

這是完美的...我沒有發現DateTimeStyles.NoCurrentDateDefault選項,這正是我需要檢測一個只有時間的輸入。我想我可能能夠逃脫僅限於日期的午夜部分...所以感謝您的幫助! – BG100

+0

@ BG100:請注意,我已經添加了一個關於如何解決「日期」和「時間」問題的示例。 –