Monday, February 16, 2009

C# :Seconds TO String

This functions return a string like 1 hours ago, a day ago etc.

//Returns seconds to 1 hour ago, 10 sec ago etc.
public static string SecondsToString(double seconds)
{

string time = "";
if (seconds >= 3600)
{
time =Convert.ToInt32((seconds / 3600)).ToString();
if (seconds > 24)
{
time = Convert.ToInt32((seconds / 24)).ToString();
time += " days ago";
}
else
time += " hrs ago";
}
else if (seconds >= 60)
{
time = Convert.ToInt32((seconds / 60)).ToString();
time += " minutes ago";
}
else
{
time = Convert.ToInt32(seconds).ToString();
time += " sec ago";
}
return time;
}

No comments: