c# - Streamline Date Parsing into method -
i'm trying think of cleaner way this. want move helper method not using out params. realize have use out params tryparse, don't have choice i'd kind of reusable method:
startdate , enddate in "yyyy/mm/dd" format , string begin , i'm parsing below.
datetime startdt; datetime enddt; startdt = (datetime.tryparse(startdate, out startdt) ? startdt : datetime.now); enddt = ((!datetime.tryparse(enddate, out enddt) || string.isnullorempty(enddate))) ? (startdt.addminutes(configuration.instance.recentorderswindowdurationminutes)) : enddt;
well, if want 2 results single method have either use out
parameters or wrapping type. if you're lazy , doing once, use tuple<datetime, datetime>
.
public tuple<datetime, datetime> getrange(string startdate, string enddate) { datetime startdt; datetime enddt; if (!datetime.tryparse(startdate, out startdt)) startdate = datetime.now; if (string.isnullorempty(enddate) || !datetime.tryparse(enddate, out enddt)) enddt = startdt.addminutes(configuration.instance.recentorderswindowdurationminutes); return new tuple<datetime, datetime>(startdt, enddt); }
if you're looking more generalized case, dealing 1 @ time, write tryparse
accepts default value.
public datetime tryparseordefault(string str, datetime def) { datetime ret; if (datetime.tryparse(str, out ret)) return ret; else return def; {
edit:
in re comment, concrete type this:
public class daterange { public datetime startdate { get; set; } public datetime enddate { get; set; } }
then use instead of tuple<datetime, datetime>
.
Comments
Post a Comment