We tend to see how many days ago, how many hours ago, in the comments.
The principle of implementation: now time-the time lag in the past to do comparison
I have defined a helper class, you can directly reference, parameter is time difference, notice time difference type is TimeSpan type, not datetime type OH ~
TimeHelper.cs
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text; Namespace time Test {///<summary>///calculates comment approximate time based on past and present two datetime times///</summary> public class Timehelper {public static string Gettimespan (TimeSpan ts) {if Math.floor (ts. Totaldays) >365) {return Math.floor (TS.
Totaldays)/365 + "Years ago"; else if (Math.floor (TS). Totaldays) >30) {return Math.floor (TS.
Totaldays)/30 + "month ago"; else if (Math.floor (TS). Totaldays) >1) {return Math.floor (TS.
totaldays) + "days ago"; else if (Math.floor (TS). totalhours) >1) {return Math.floor (TS.
totalhours) + "hours ago"; else if (Math.floor (TS). totalminutes) >1) {return Math.floor (TS.
totalminutes) + "Minutes ago"; } else {return Math.floor (TS.
TotalSeconds) + "seconds ago"; }
}
}
}