Get Date Difference in String
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
class Main{
public static string GetDifferenceDate(DateTime TweetDate, DateTime NowDate)
{
if (DateTime.Compare(NowDate, TweetDate) >= 0)
{
string TimeFormat = string.Empty;
TimeSpan ts = NowDate.Subtract(TweetDate);
//Default
TimeFormat = string.Format("{0} seconds ago", ts.Seconds);
if (ts.Days > 0)
{
if (ts.Days < 2)
{
TimeFormat = "Yesterday";
}
else
{
TimeFormat = string.Format("{0} days ago", ts.Days);
}
}
if (ts.Days < 1 && ts.Hours > 0)
{
if (ts.Hours < 2)
{
TimeFormat = "About an hour ago";
}
else
{
TimeFormat = string.Format("{0} hours ago ", ts.Hours);
}
}
if (ts.Days < 1 && ts.Hours < 1 && ts.Minutes > 0)
{
TimeFormat = string.Format("{0} minutes ago", ts.Minutes);
}
if (ts.Days < 1 && ts.Hours < 1 && ts.Minutes < 1 && ts.Seconds > 0)
{
TimeFormat = string.Format("{0} seconds ago", ts.Seconds);
}
return TimeFormat;
}
else
return "Not valid";
}
}
}
Related examples in the same category