c# - Store difference between two dates recursively and calculate time spent on web site -
i have find time spent on site choose maintain 2 datetimes , time in database can save difference between login time , logout time , store in table column timespent
.
the time stored {00:16:58.413785200}
have add amount of time recursively after each logout if spent 10 minutes in first login , 5 minutes in 2nd login have store 15 min timspent on site.
but problem not able guess happens if want add 2 minutes after 23:59:59.000000000. know max time value 23:59:59 can stored time datatype in sql.
so how achieve this? have tried
var _user = db.users.singleordefault(t => t.id == id); _user.endtime = datetime.now; var _starttime = _user.starttime; if (_user.timespent != null) { _user.timespent = _user.timespent + (_user.endtime - _starttime); } else { _user.timespent = (_user.endtime - _starttime); } db.savechanges();
you need switch datatype on timespent
column bigint
. should have default value of 0 timespent
column aswell or handle in code.
var _user = db.users.singleordefault(t => t.id == id); _user.endtime = datetime.now; var _starttime = _user.starttime; timespan timespent = _user.timespent != null ? timespan.fromticks(_user.timespent) : 0; if (timespent > timespan.zero) { timespent = timespent + (_user.endtime - _starttime) } else { timespent = _user.endtime - _starttime } _user.timespent = timespent.ticks; db.savechanges();
Comments
Post a Comment