c# - How to find the exact time left -
i working on reminder app , have taken 2 properties 1 date in datetime format , time of type string .my problem not able exact hours , min , sec when user set reminder future date how subtract future time current time can exact hours , min , sec left. please me out
thank here model class
private datetime _date=datetime.today.date; public datetime date { { return _date; } set { timespan tp = value - datetime.now; if (tp.totaldays>0) { remainingdays = (int)tp.totaldays; //timespan ts = value- system.timespan.fromhours(time); //string.format("{0}:{1}", system.math.truncate(ts.totalhours).tostring(), ts.minutes.tostring()); remaininghours = (int)tp.totalhours; remainingmin = (int)tp.minutes; remainingsec = (int)tp.seconds; setproperty(ref _date, value); } } } private string _time = datetime.now.timeofday.tostring(); public string time { { return _time; } set { setproperty(ref _time, value); } } private int _remainingdays; public int remainingdays { { return _remainingdays; } set { setproperty(ref _remainingdays, value); } } private int _remaininghours; public int remaininghours { { return _remaininghours; } set { setproperty(ref _remaininghours, value); } } private int _remainingmin; public int remainingmin { { return _remainingmin; } set { setproperty(ref _remainingmin, value); } } private int _remainingsec; public int remainingsec { { return _remainingsec; } set { setproperty(ref _remainingsec, value); } } }
} ui have show time left
<stackpanel orientation="horizontal" height="60" horizontalalignment="center" > <textblock x:name="tbkremaningtime" fontsize="50" > <run text="{binding remaininghours}"/> <run text=":"/> <run text="{binding remainingmin}"/> <run text=":"/> <run text="{binding remainingsec}"/> </textblock> </stackpanel>
let's take step back. using property setter in untraditional way. consider client writes code:
var y = new yourobject(); console.writeline(y.date); // prints today's date y.date = new datetime(2015,1,1); console.writeline(y.date); // still prints today's date (?!)
setting y.date
didn't change y.date
@ all! mutated several other fields/properties. violates core property design principle properties "should seen smart fields", not mention principle of least surprise.
moreover, instead of getting timespan
-- well-documented type describing interval between 2 dates -- forcing client use own, limited, methods same data.
instead, consider:
class yourobject { public datetime date {get; set;} public timespan timeuntildate { {return date - datetime.now;} } }
and let client use resultant timespan
necessary.
Comments
Post a Comment