Diff For Humans - CSharp System

CSharp examples for System:DateTime Format

Description

Diff For Humans

Demo Code


using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using System;//w ww .  ja va2s.  co  m

public class Main{
        public static string DiffForHumans(this DateTime dateTime)
        {
            var time = DateTimeSpan.CompareDates(dateTime, DateTime.UtcNow);
            if (time.Years != 0)
                return $"{time.Years} years ago";
            if (time.Months != 0)
                return $"{time.Months} months ago";
            if (time.Days != 0)
                return $"{time.Days} days ago";
            if (time.Hours != 0)
                return $"{time.Hours} hours ago";
            if (time.Minutes != 0)
                return $"{time.Minutes} minutes ago";
            return "Just now";
        }
}

Related Tutorials