Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# 2 timespan return yesterday

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 - yourDate.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";
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to change an object color with fill c# 
Csharp :: c# stringwriter encoding iso-8859-1 example 
Csharp :: set data annotation text in model c# 
Csharp :: C# if...else Statement 
Csharp :: unity check if transform doent have parent 
Csharp :: c# unzip all archive files inside directory 
Csharp :: hahhaa i hack u 
Csharp :: c# ipaddress to integer 
Csharp :: how to split string into a list ignoring number of spaces c# 
Csharp :: c# (sharp) varibles 
Csharp :: c# generic type converter 
Csharp :: unity how to have multiple headers 
Csharp :: viewsheet location revit api 
Csharp :: Thread.Sleep() without freezing the UI 
Csharp :: math round to next integer c# 
Csharp :: keep sprites at fixed transform according to screen resolution unity 
Csharp :: itext7 c# memorystream 
Csharp :: populate toolstripitems to combobox 
Csharp :: Visual Studio - Summary Tag Comments - Optional Params 
Csharp :: what is C# 
Csharp :: convert list of object linq 
Csharp :: ExceptionFilterAttribute exception-handler-middleware-not-catching 
Csharp :: how to mock abstract httpcontext using moq .net core 
Csharp :: C# how to stop user type into combobox 
Csharp :: Delete last modification on EntityFramework Core 
Csharp :: Worker service as Windows Service 
Csharp :: report background worker 
Csharp :: how to get connection string from xml file in c# 
Csharp :: c# force arguments to be keywords 
Csharp :: unity async await 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =