c# - Converting string to DateTime safely -
i have string came database, i´m not sure valid datetime.
first i´m trying validate if it´s null,
then if not null want safely convert datetime because i´m not sure row["a"].tostring valid datetime
output.limitexpirydate = row["a"] == dbnull.value ? datetime.now : "something convert here";
does have idea?
you can utilize datetime.tryparse
:
output.limitexpirydate = datetime.now; datetime limitexpirydate; if(row["a"] != dbnull.value && datetime.tryparse(row.field<string>("a"), out limitexpirydate)) output.limitexpirydate = limitexpirydate;
it nullable datetime
column utilize field
method supports nullable types:
datetime? limitexpirydate = row.field<datetime?>("a"); if(limitexpirydate.hasvalue) output.limitexpirydate = limitexpirydate.value;
c# .net type-conversion
No comments:
Post a Comment