Hi Friendz,
Often we feel the need to convert a datetime value to a formatted string.
For E.g.:suppose I have a datetime value 3 hours past current time. while displaying it to user in a more user Friendly way,I want to display it something like this. "3 hours Ago".
I have written an if else statement which does this job.
private string manageTime(string dtconvert)
{
TimeSpan ts;
ts = DateTime.Now.Subtract(Convert.ToDateTime(dtconvert));
//------------------------------------------------------------------------
string temp = (ts.Days > 0) ? ((ts.Days > 364) ? (((ts.Days / 365) == 1) ? Convert.ToString(ts.Days / 365) + " year ago" : Convert.ToString(ts.Days / 365) + " years ago")
: (ts.Days > 29) ? (((ts.Days / 30) == 1) ? Convert.ToString(ts.Days / 30) + " month ago" : Convert.ToString(ts.Days / 30) + " months ago")
: (ts.Days == 1 ? Convert.ToString(ts.Days) + " day ago" : Convert.ToString(ts.Days) + " days ago"))
: ((ts.Hours > 0) ? ((ts.Hours == 1) ? Convert.ToString(ts.Hours) + " hour ago" : Convert.ToString(ts.Hours) + " hours ago")
: ((ts.Minutes > 0) ? ((ts.Minutes == 1) ? Convert.ToString(ts.Minutes) + " min ago" : Convert.ToString(ts.Minutes) + " mins ago")
: ((ts.Seconds == 1) ? Convert.ToString(ts.Seconds) + " sec ago" : Convert.ToString(ts.Seconds) + " secs ago")));
return temp;
}
Hope you will like it !!!
Till my next post,Good bye ....Take Care :-)
Tuesday, June 23, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment