Mi scuso se questo è stato chiesto prima. Ho alcuni dati che ho bisogno di memorizzare come stringhe, alcuni dei quali dati sono date. I dati iniziano come stringhe come "01/02/10" (formato uk). Ora, più avanti, questi dati vengono analizzati e, a seconda di cosa fa l'analisi, i risultati sono diversi (01-Feb-10 vs 02-Jan-10 per esempio). Dato che i dati iniziano come stringhe, prima di memorizzarlo vorrei dire, "se questa sembra una data, formattala come gg-mmm-aa".Scrittura di un equivalente IsDate() in C#?
Il problema è che molte cose sembrano una data per la funzione DateTime.Parse().
Quindi, ho applicato alcune regole e accettato solo i formati di data "ragionevole" per i miei assegni, e ho scritto una funzione IsDate(). Sto cercando suggerimenti su come farlo perché, mentre funziona, la mia soluzione sembra molto goffa.
L'intero motivo per cui ho eseguito questa operazione anziché seguire la solita routine DateTime.TryParse è chiaro se avete mai iniziato a lanciare stringhe casuali (come "3/4" e "6.12").
Ecco quello che ho finora:
class Program
{
static void Main(string[] args)
{
Debug.Assert(IsDate(6.12) == false);
Debug.Assert(IsDate("3/4") == false);
Debug.Assert(IsDate(010210) == false);
Debug.Assert(IsDate("010210") == false);
Debug.Assert(IsDate("12-jan-2000") == true);
Debug.Assert(IsDate("12-12-20") == true);
Debug.Assert(IsDate("1/1/34") == true);
Debug.Assert(IsDate("09/30/20") == false);
Debug.Assert(IsDate(DateTime.Now) == true);
}
static Boolean IsDate(Object value)
{
DateTimeFormatInfo DateTimeFormatGB = new CultureInfo("en-GB").DateTimeFormat; // new CultureInfo("en-US").DateTimeFormat;
return IsDate(value, DateTimeFormatGB);
}
static private List<String> AcceptableDateFormats = new List<String>(72);
static Boolean IsDate(Object value, DateTimeFormatInfo formatInfo)
{
if (AcceptableDateFormats.Count == 0)
{
foreach (var dateFormat in new[] { "d", "dd" })
{
foreach (var monthFormat in new[] { "M", "MM", "MMM" })
{
foreach (var yearFormat in new[] { "yy", "yyyy" })
{
foreach (var separator in new[] { "-", "/" }) // formatInfo.DateSeparator ?
{
String shortDateFormat;
shortDateFormat = dateFormat + separator + monthFormat + separator + yearFormat;
AcceptableDateFormats.Add(shortDateFormat);
AcceptableDateFormats.Add(shortDateFormat + " " + "HH:mm"); // formatInfo.TimeSeparator
AcceptableDateFormats.Add(shortDateFormat + " " + "HH:mm:ss");
}
}
}
}
}
String sValue = value.ToString().Trim();
DateTime unused;
foreach (String format in AcceptableDateFormats)
{
if (DateTime.TryParseExact(sValue, format, formatInfo, DateTimeStyles.None, out unused) == true) return true;
}
return false;
}
}
non ho usato i separatori di data/ora dalle informazioni cultura perché volevo accettare sia un "/" e un "-". Suppongo che avrei potuto usare quello per ora, dato che è improbabile che cambi (per me).
Perché non utilizzare DateTime.TryParse, quindi eseguire un controllo di integrità, ad esempio sulla parte relativa all'anno del DateTime restituito? –