{"id":4291,"date":"2020-11-18T12:23:29","date_gmt":"2020-11-18T11:23:29","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=4291"},"modified":"2024-10-15T15:21:49","modified_gmt":"2024-10-15T14:21:49","slug":"c-calculate-display-relative-time","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-calculate-display-relative-time\/","title":{"rendered":"C# calculate \/ display relative time"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">namespace Domain.Helpers\n{\n    public static class RelativeTimeHelper\n    {\n        public static List&lt;Threshold> Thresholds = new()\n        {\n            new Threshold((d) => d.AddMinutes(-1), \"{0} seconden\", x => x.Seconds),\n            new Threshold((d) => d.AddMinutes(-2), \"\u00e9\u00e9n minuut \", x => x.Minutes),\n            new Threshold((d) => d.AddMinutes(-60), \"{0} minuten\", x => x.Minutes),\n            new Threshold((d) => d.AddMinutes(-120), \"\u00e9\u00e9n uur\", x => x.Hours),\n            new Threshold((d) => d.AddDays(-1), \"{0} uur\", x => x.Hours),\n            new Threshold((d) => d.AddDays(-2), \"gisteren\", x => x.Days),\n            new Threshold((d) => d.AddMonths(-1), \"{0} dagen\", x => x.Days),\n            new Threshold((d) => d.AddMonths(-2), \"{0} maand\", x => x.Months),\n            new Threshold((d) => d.AddYears(-1), \"{0} maanden\", x => x.Months),\n            new Threshold((d) => DateTime.MaxValue, \"{0} jaar\", x => x.Years)\n        };\n\n        public static string RelativeDateTime(DateTime theDate) => FriendlyTimespan(theDate);\n        public static string FriendlyTimespan(DateTime date, DateTime? now = null)\n        {\n            var current = now ?? DateTime.Now;\n            var threshold = Thresholds.OrderByDescending(x => x.Condition(current)).FirstOrDefault(x => x.Condition(current) &lt; date) ?? Thresholds.Last();\n            var tresholdRelativeValues = TresholdRelativeValues.Get(date, current);\n            return string.Format(threshold.Description, threshold.Converter(tresholdRelativeValues));\n        }\n    }\n\n    public record TresholdRelativeValues(int Seconds, int Minutes, int Hours, int Days, int Months, int Years)\n    {\n        internal static TresholdRelativeValues Get(DateTime date, DateTime now)\n        {\n            var d = now - date;\n            (var totalYears, var totalMonths )= GetYearAndMonthDifference(date, now);\n            \/\/= GetMonthDifference(date, now);\n            return new TresholdRelativeValues((int)Math.Ceiling(d.TotalSeconds), (int)Math.Ceiling(d.TotalMinutes), (int)Math.Ceiling(d.TotalHours), (int)Math.Ceiling(d.TotalDays), totalMonths, totalYears);\n        }\n\n        static (int Years, int Months) GetYearAndMonthDifference(DateTime start, DateTime end)\n        {\n            \/\/ Bereken het aantal jaren en maanden verschil\n            int yearsDifference = end.Year - start.Year;\n            int monthsDifference = end.Month - start.Month;\n\n            \/\/ Als de maand van de einddatum v\u00f3\u00f3r de maand van de startdatum valt, betekent dat dat er nog geen volledig jaar is verstreken\n            if (monthsDifference &lt; 0)\n            {\n                yearsDifference--;\n                monthsDifference += 12; \/\/ Correctie voor negatieve maandverschillen\n            }\n\n            \/\/ Controleer of de einddatum v\u00f3\u00f3r de dag van de startdatum in de huidige maand valt\n            if (end.Day &lt; start.Day)\n            {\n                monthsDifference--;\n\n                \/\/ Als het maandverschil nu negatief is, trek dan ook \u00e9\u00e9n jaar af en corrigeer het maandverschil\n                if (monthsDifference &lt; 0)\n                {\n                    yearsDifference--;\n                    monthsDifference += 12;\n                }\n            }\n\n            return (yearsDifference, monthsDifference);\n        }\n    }\n    public class Threshold\n    {\n        public Threshold(Func&lt;DateTime, DateTime> condition, string description, Func&lt;TresholdRelativeValues, int> converter)\n        {\n            Condition = condition;\n            Description = description;\n            Converter = converter;\n        }\n        public Func&lt;DateTime, DateTime> Condition { get; }\n        public string Description { get; }\n        public Func&lt;TresholdRelativeValues, int> Converter { get; }\n    }\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-4291","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=4291"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4291\/revisions"}],"predecessor-version":[{"id":8966,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4291\/revisions\/8966"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=4291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=4291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=4291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}