Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 answers

573 users

How to calculate past relative time given a specific DateTime in C#

1 Answer

0 votes
using System;

public class Program
{
    public static string RelativePastTime(DateTime userdate) {
        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 - userdate.Ticks);
        double delta = Math.Abs(ts.TotalSeconds);

        if (delta < 1 * MINUTE) {
            return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
        }

        if (delta < 2 * MINUTE) {
            return "a minute ago";
        }

        if (delta < 45 * MINUTE) {
            return ts.Minutes + " minutes ago";
        }
        
        if (delta < 90 * MINUTE) {
            return "an hour ago";
        }
        
        if (delta < 24 * HOUR) {
            return ts.Hours + " hours ago";
        }
        
        if (delta < 48 * HOUR) {
            return "yesterday";
        }
        
        if (delta < 30 * DAY) {
            return ts.Days + " days ago";
        }
        
        if (delta < 12 * MONTH) {
            int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
            
            return months <= 1 ? "one month ago" : months + " months ago";
        }
        else {
            int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
          
            return years <= 1 ? "one year ago" : years + " years ago";
        }
    }

    public static void Main(string[] args)
    {
        Console.WriteLine(DateTime.UtcNow); 
                
        Console.WriteLine("A: " + RelativePastTime(new DateTime(2024,04,23,00,00,00)));
        Console.WriteLine("B: " + RelativePastTime(new DateTime(2024,04,24,00,00,00)));
        Console.WriteLine("C: " + RelativePastTime(new DateTime(2024,03,25,00,00,00)));
        Console.WriteLine("D: " + RelativePastTime(new DateTime(2024,04,25,17,15,00)));
        Console.WriteLine("E: " + RelativePastTime(new DateTime(2024,04,25,00,15,00)));
        Console.WriteLine("F: " + RelativePastTime(new DateTime(2024,04,25,18,21,30))); 
        Console.WriteLine("G: " + RelativePastTime(new DateTime(2023,04,25,00,00,00))); 
        Console.WriteLine("H: " + RelativePastTime(new DateTime(2024,04,26,07,07,21))); 
        Console.WriteLine("I: " + RelativePastTime(new DateTime(2024,04,26,07,06,11))); 
    }
}


 
/*
run:
     
04/26/2024 07:08:16
A: 3 days ago
B: 2 days ago
C: one month ago
D: 13 hours ago
E: yesterday
F: 12 hours ago
G: one year ago
H: 55 seconds ago
I: 2 minutes ago
 
*/

 



answered Apr 25, 2024 by avibootz
edited Apr 26, 2024 by avibootz

Related questions

1 answer 141 views
1 answer 181 views
3 answers 160 views
1 answer 132 views
2 answers 134 views
...