C# ile göreceli zaman hesaplama
const int SECOND = 1;
const int MINUTE = 60 * SECOND;
const int HOUR = 60 * MINUTE;
const int DAY = 24 * HOUR;
const int MONTH = 30 * DAY;
var ts = new TimeSpan(DateTime.UtcNow.Ticks - yourDate.Ticks);
// var ts = new TimeSpan(DateTime.UtcNow.AddHours(3).Ticks - yourDate.Ticks); türkiye için bu satırı kullanmalısınız
double delta = Math.Abs(ts.TotalSeconds);
if (delta < 1 * MINUTE)
return ts.Seconds == 1 ? "
bir saniye önce" : ts.Seconds + "
saniye önce";
if (delta < 2 * MINUTE)
return "
bir dakika önce";
if (delta < 45 * MINUTE)
return ts.Minutes + "
dakika önce";
if (delta < 90 * MINUTE)
return "
bir saat önce";
if (delta < 24 * HOUR)
return ts.Hours + "
saat önce";
if (delta < 48 * HOUR)
return "dün";
if (delta < 30 * DAY)
return ts.Days + " gün önce";
if (delta < 12 * MONTH)
{
int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
return months <= 1 ? "bir ay önce" : months + " ay önce";
}
else
{
int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
return years <= 1 ? "bir yıl önce" : years + " yıl önce";
}