Sometime we need to convert DateTime to time spams like an hour ago, few minutes ago or seconds ago.
public string convertDate(DateTime dt)
{
string result = "";
TimeSpan span = DateTime.Now - dt;
int days = span.Days;
int hrs = span.Hours;
int min = span.Minutes;
int sec = span.Seconds;
if (days!=0)
{
result = days + " days ago";
}
else if (hrs >= 1)
{
result = hrs + " hours ago";
}
else if (min <= 59 && min>0)
{
result = min + " minutes ago";
}
else
{
result = sec + " seconds ago";
}
return result;
}
0 comments:
Post a Comment