From cc7f50e4e739506cb3633d8626b9c69fd6877fc1 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Fri, 7 Sep 2018 09:51:43 +0300 Subject: [PATCH] Blog module Added ConvertDatetimeToTimeAgo method --- .../Resources/Blogging/Web/en.json | 7 +++ .../Resources/Blogging/Web/tr.json | 7 +++ .../Pages/Blog/BloggingPage.cs | 44 ++++++++++++++++++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/modules/blogging/src/Volo.Blogging.Web/Localization/Resources/Blogging/Web/en.json b/modules/blogging/src/Volo.Blogging.Web/Localization/Resources/Blogging/Web/en.json index 8b0ab73c2b..5b3e3c470b 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Localization/Resources/Blogging/Web/en.json +++ b/modules/blogging/src/Volo.Blogging.Web/Localization/Resources/Blogging/Web/en.json @@ -5,6 +5,13 @@ "Title": "Title", "ContinueReading": "Continue Reading", "DaysAgo": "days ago", + "YearsAgo": "years ago", + "MonthsAgo": "months ago", + "WeeksAgo": "weeks ago", + "MinutesAgo": "minutes ago", + "SecondsAgo": "seconds ago", + "HoursAgo": "hours ago", + "Now": "now", "Content": "Content", "SeeAll": "See All", "PopularTags": "Popular Tags", diff --git a/modules/blogging/src/Volo.Blogging.Web/Localization/Resources/Blogging/Web/tr.json b/modules/blogging/src/Volo.Blogging.Web/Localization/Resources/Blogging/Web/tr.json index be713a517b..8d1cd8f056 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Localization/Resources/Blogging/Web/tr.json +++ b/modules/blogging/src/Volo.Blogging.Web/Localization/Resources/Blogging/Web/tr.json @@ -5,6 +5,13 @@ "Title": "Başlık", "ContinueReading": "Devamı...", "DaysAgo": "Gün Önce", + "YearsAgo": "yıl önce", + "MonthsAgo": "ay önce", + "WeeksAgo": "hafta önce", + "MinutesAgo": "dakika önce", + "SecondsAgo": "saniye önce", + "HoursAgo": "saat önce", + "Now": "şimdi", "Content": "İçerik", "SeeAll": "Hepsini Gör", "PopularTags": "Popüler Etiketler", diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/BloggingPage.cs b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/BloggingPage.cs index 7a07772219..1adcc10e8b 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/BloggingPage.cs +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/BloggingPage.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc.Localization; +using System; +using Microsoft.AspNetCore.Mvc.Localization; using Microsoft.AspNetCore.Mvc.Razor.Internal; using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; using Volo.Blogging.Localization; @@ -21,5 +22,46 @@ namespace Volo.Blogging.Pages.Blog return title; } + + public string ConvertDatetimeToTimeAgo(DateTime dt) + { + var timeDiff = DateTime.Now - dt; + + var diffInDays = (int) timeDiff.TotalDays; + + if (diffInDays >= 365) + { + return diffInDays / 365 + L["YearsAgo"].Value; + } + if (diffInDays >= 30) + { + return diffInDays / 30 + L["MonthsAgo"].Value; + } + if (diffInDays >= 7) + { + return diffInDays / 7 + L["WeeksAgo"].Value; + } + if (diffInDays >= 1) + { + return diffInDays + L["DaysAgo"].Value; + } + + var diffInSeconds = (int) timeDiff.TotalSeconds; + + if (diffInSeconds >= 3600) + { + return diffInSeconds / 3600 + L["HoursAgo"].Value; + } + if (diffInSeconds >= 60) + { + return diffInSeconds / 60 + L["MinutesAgo"].Value; + } + if (diffInSeconds >= 1) + { + return diffInSeconds + L["SecondsAgo"].Value; + } + + return L["Now"].Value; + } } }