Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Relative Date

Posted on February 27, 2013October 26, 2015 By David Kittell
	public static string RelativeDate(DateTime theDate)
	{
		Dictionary<long, string> thresholds = new Dictionary<long, string>();
		int minute = 60;
		int hour = 60 * minute;
		int day = 24 * hour;
		thresholds.Add(60, "{0} seconds ago");
		thresholds.Add(minute * 2, "a minute ago");
		thresholds.Add(45 * minute, "{0} minutes ago");
		thresholds.Add(120 * minute, "an hour ago");
		thresholds.Add(day, "{0} hours ago");
		thresholds.Add(day * 2, "yesterday");
		thresholds.Add(day * 30, "{0} days ago");
		thresholds.Add(day * 365, "{0} months ago");
		thresholds.Add(long.MaxValue, "{0} years ago");

		long since = (DateTime.Now.Ticks - theDate.Ticks) / 10000000;
		foreach (long threshold in thresholds.Keys)
		{
			if (since < threshold)
			{
				TimeSpan t = new TimeSpan((DateTime.Now.Ticks - theDate.Ticks));
				return string.Format(thresholds[threshold], (t.Days > 365 ? t.Days / 365 : (t.Days > 0 ? t.Days : (t.Hours > 0 ? t.Hours : (t.Minutes > 0 ? t.Minutes : (t.Seconds > 0 ? t.Seconds : 0))))).ToString());
			}
		}
		return "";
	}
Originally Posted on February 27, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code CSharp

Post navigation

Previous post
Next post

Related Posts

UNIX – SED Remove Comment Lines

Posted on January 23, 2017

This script will remove all lines that start with # along with all comments. Whatever file you choose I suggest you view first then execute sed "/^\s*#/d;s/\s*#[^\"’]*$//" /etc/dnsmasq.conf && sed ‘/^\s*$/d’ /etc/dnsmasq.conf sudo sed -i "/^\s*#/d;s/\s*#[^\"’]*$//" /etc/dnsmasq.conf && sudo sed -i ‘/^\s*$/d’ /etc/dnsmasq.conf All information on this site is shared…

Read More

UNIX – Laptop Setup

Posted on April 9, 2018April 9, 2018

This is my basic setup for a laptop/desktop that I have for my family to use. Debian Based Should work on Debian, Ubuntu (all Ubuntu flavors) sudo apt-get –assume-yes install ntp sudo timedatectl set-timezone America/Detroit sudo service ntp stop sudo ntpdate us.pool.ntp.org sudo service ntp start echo nameserver 208.67.222.123 >…

Read More

Azure Red Hat – RHUI Update

Posted on December 27, 2016January 9, 2017

Most of the this script is based on the document “Red Hat Update Infrastructure (RHUI) for on-demand Red Hat Enterprise Linux VMs in Azure” at https://docs.microsoft.com/en-us/azure/virtual-machines/virtual-machines-linux-update-infrastructure-redhat?toc=%2fazure%2fvirtual-machines%2flinux%2ftoc.json My change will first check if the update was already installed, if not it will follow the process to install See updates at: https://gitlab.com/Kittell-RedHat/AzureRepo/raw/master/AzureRedHatRepoUpdate.sh…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes