What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 45365 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > ASP.NET > Method to Get Time Elapsed With Refernce to Current Date Time ...
Lakhangarg

Method to Get Time Elapsed With Refernce to Current Date Time

 Code Snippet posted by: Lakhangarg | Posted on: 9/29/2009 | Category: ASP.NET Codes | Views: 1089 | Status: [Member] [Moderator] | Alert Moderator   


With the help of following code we can get the time elapsed with refernce to current date time.

Pass the Previous Activities Date Time and the following function will return time elapsed in the format:
{Number} hour/hours ago,{Number} Day/Days ago,{Number} week/weeks ago,{Number} month/months ago

public static string GetDaysAgo(string strCreatedDateTime)

{
try
{
DateTime CreatedDateTime = Convert.ToDateTime(strCreatedDateTime);
string StrReturn = null;
TimeSpan TimeDiff = DateTime.Now - CreatedDateTime;
double MinDiff = Convert.ToDouble(TimeDiff.TotalMinutes.ToString());
if (MinDiff < 0) MinDiff = 0;
if (MinDiff < 60) StrReturn = Math.Floor(Convert.ToDecimal(MinDiff)).ToString() + " minutes ago";
else { MinDiff = MinDiff / 60;
if (MinDiff < 24) if (Math.Floor(Convert.ToDecimal(MinDiff)) == 1) StrReturn = Math.Floor(Convert.ToDecimal(MinDiff)).ToString() + " hour ago";
else StrReturn = Math.Floor(Convert.ToDecimal(MinDiff)).ToString() + " hours ago";
else { MinDiff = MinDiff / 24;
if (MinDiff < 7) if (Math.Floor(Convert.ToDecimal(MinDiff)) == 1) StrReturn = Math.Floor(Convert.ToDecimal(MinDiff)).ToString() + " day ago";
else StrReturn = Math.Floor(Convert.ToDecimal(MinDiff)).ToString() + " days ago";
else if (MinDiff < 30) { MinDiff = MinDiff / 7;
if (Math.Floor(Convert.ToDecimal(MinDiff)) == 1) StrReturn = Math.Floor(Convert.ToDecimal(MinDiff)).ToString() + " week ago";
else StrReturn = Math.Floor(Convert.ToDecimal(MinDiff)).ToString() + " weeks ago";
} else { MinDiff = MinDiff / 30;
if (Math.Floor(Convert.ToDecimal(MinDiff)) == 1) StrReturn = Math.Floor(Convert.ToDecimal(MinDiff)).ToString() + " month ago";
else StrReturn = Math.Floor(Convert.ToDecimal(MinDiff)).ToString() + " months ago";
} } } return StrReturn;
} catch (Exception ex) { return "1 months ago";
} }


Call this function like this:
GetDaysAgo("30/8/2009");
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 8:24:11 AM