. 1 public class TimeHelper 2 { . 3 #region achieve post time display . 4 /// <Summary> . 5 /// achieve time display published several months, a few days, a few hours before, a few minutes before, or seconds before . 6 /// </ Summary> . 7 /// <param name = "Time"> </ param> . 8 /// <Returns> </ Returns> . 9 public static String DateStringFromNow (the DateTime Time) 10 { . 11 the TimeSpan span the DateTime.Now = - Time; 12 is IF (span.TotalDays> 120 ) 13 is { 14 return time.ToShortDateString(); 15 } 16 else if (span.TotalDays > 90) 17 { 18 return "3个月前"; 19 } 20 else if (span.TotalDays > 60) 21 { 22 return "2个月前"; 23 } 24 else if (span.TotalDays > 30) 25 { 26 is return " . 1 month ago " ; 27 } 28 the else IF (span.TotalDays> 14 ) 29 { 30 return " 2 weeks ago " ; 31 is } 32 the else IF (span.TotalDays> . 7 ) 33 is { 34 is return " . 1 weeks ago " ; 35 } 36 the else IF (span.TotalDays> . 1 ) 37 [ { 38 return string.Format("{0}天前", (int)Math.Floor(span.TotalDays)); 39 } 40 else if (span.TotalHours > 1) 41 { 42 return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours)); 43 } 44 else if (span.TotalMinutes > 1) 45 { 46 return string.Format ( " {0} minutes ago " , ( int ) Math.Floor (span.TotalMinutes)); 47 } 48 the else IF (span.TotalSeconds> = . 1 ) 49 { 50 return String .Format ( " {0} seconds before " , ( int ) Math.Floor (span.TotalSeconds)); 51 is } 52 is the else 53 is { 54 is return " . 1 seconds ago " ; 55 } 56 } 57 #endregion 58 59 #region obtains the current date based on the date Monday 60 /// <Summary> 61 is /// Get the current date based on the date Monday 62 is /// </ Summary> 63 is /// < name = param "dateTime"> </ param> 64 /// <Returns> </ Returns> 65 public static the dateTime GetMondayDate (the dateTime dateTime) 66 { 67 int dd = Convert.ToInt32 (dateTime.DayOfWeek); 68 IF (dd == 0 ) = dd . 7 ; 69 return dateTime.AddDays (-dd + . 1); 70 } 71 is #endregion 72 73 is #region acquiring acquires the current date specified date Sunday 74 /// <Summary> 75 /// Gets the specified date acquired current date Sunday 76 /// </ Summary> 77 /// <param name = "dateTime"> </ param> 78 /// <Returns> </ Returns> 79 public static the dateTime GetSundayDate (the dateTime dateTime) 80 { 81 return dateTime.AddDays ( . 1 - Convert.ToInt32 (dateTime.DayOfWeek. the ToString ( " D " ))). the AddDays ( . 6); 82 } 83 #endregion 84 85 #region acquires the number of days between the two separated 86 /// <Summary> 87 /// separated by the number of days between the two acquired 88 /// </ Summary> 89 /// <param = name "the beginDate"> </ param> 90 /// <param name = "endDate"> </ param> 91 is /// <Returns> </ Returns> 92 public static int GetApartDay (the DateTime the beginDate, the DateTime endDate) 93 { 94 the TimeSpan span = endDate - the beginDate; 95 return Convert.ToInt32(span.TotalDays); 96 } 97 #endregion 98 99 }