How to get posted time ago in C#?
Here is function which return you ago:
public static string GetPostedTime(DateTime postedDateTime)
{
string msg = string.Empty;
var crrentDateTime = DateTime.Now;
var postedDateTimeTemap = DateTime.Now.AddMinutes(-8);
DateTime.TryParse(postedDateTime.ToString(), out postedDateTimeTemap);
var diff = crrentDateTime.Subtract(postedDateTimeTemap);
if (diff.Days >= 365)
{
msg = diff.Days / 365 + " years ago";
}else if(diff.Days >= 30)
{
msg = diff.Days / 30 + " month ago";
} else if (diff.Days > 0)
{
msg = diff.Days.ToString() + " days ago";
} else if (diff.Hours > 0)
{
msg = diff.Hours.ToString() + " hours ago";
} else if (diff.Minutes > 0)
{
msg = diff.Minutes.ToString() + " minuts ago";
} else
{
msg = diff.Seconds.ToString() + " seconds ago";
}
return msg;
}
Share This with your friend by choosing any social account