c# - Get seconds minutes hours days weeks months years from a date -
i method returns string date looks ones in social media websites , forum .
examples
- added 2 seconds ago
- added 3 mintues ago
- added 2 weeks ago
- added month ago
and on
i pass datetime object , returns string based on difference between currentdate , date passed method.
i know how use timespan difference between dates how can switch seconds minutes hours days weeks appropriately?
for example if difference between dates 120 minutes not want return 120 minutes, want return 2 hours.
how make switch?
i prefer custom method in order change language of string arabic. can language changed using libraries?
thanks abdullah
this overly simplified example of do, using .net classes:
public static string timesinceevent(datetime eventtime) { timespan timesince = datetine.now - eventtime; if (timesince.hours > 0) return string.format("added {0} hours ago", timesince.hours); else if (timesince.minutes > 0) return string.format("added {0} minutes ago", timesince.minutes); else return string.format("added {0} seconds ago", timesince.seconds); } of course, add more cases handle days, months, etc.
Comments
Post a Comment