.Net C# dilinde DateTime değer nasıl aşağıdaki gibi bir formatta görüntülenir?
Toplam 1 cevap
C# ile DateTime veriyi "1 saat önce" , "1 gün önce " gibi algılaması daha kolay bir düzene çevirmek için aşağıdaki yardımcı sınıfı kullanabilirsiniz. Bu kodu Class olarak projenize eklediğinizde
TarihSaatVerisi.HosGorunumluTarihSaat();
şeklinde kullanabilirsiniz.
public class TarihSaatYardimcisi
{
private const string saniyeOnce = "{0} saniye önce";
private const string dakikaOnce = "{0} dakika önce";
private const string saatOnce = "{0} saat önce";
private const string gunOnce = "{0} gün önce";
private const string haftaOnce = "{0} hafta önce";
private const string ayOnce = "{0} ay önce";
private const string yilOnce = "{0} yıl önce";
public static string HosGorunumluTarihSaat(this DateTime zaman)
{
var timeDifference = DateTime.Now.Subtract(zaman);
if (timeDifference.TotalSeconds < 60)
{
return String.Format(saniyeOnce, Math.Floor(timeDifference.TotalSeconds));
}
if (timeDifference.TotalMinutes < 60)
{
return String.Format(dakikaOnce, Math.Floor(timeDifference.TotalMinutes));
}
if (timeDifference.TotalHours < 24)
{
return String.Format(saatOnce, Math.Floor(timeDifference.TotalHours));
}
if (timeDifference.TotalDays < 7)
{
return String.Format(gunOnce, Math.Floor(timeDifference.TotalDays));
}
if (timeDifference.TotalDays < 30)
{
return String.Format(haftaOnce, Math.Floor(timeDifference.TotalDays / 7));
}
if (timeDifference.TotalDays <= 365)
{
return String.Format(ayOnce, Math.Floor(timeDifference.TotalDays / 30));
}
if (timeDifference.TotalDays >= 365)
{
return String.Format(yilOnce, Math.Floor(timeDifference.TotalDays / 365));
}
return zaman.ToShortDateString();
}
}