In QQ and other space, we often see such a dynamic release and how many seconds ago (32 seconds ago), 1 minutes ago, 1 hours ago, 1 days ago, 1 months ago, but our database in the format of the time to save: 2016-05-18 00:19:21.437-to go to distress and can not be converted, A method is provided below.
/// <summary> /// /// </summary> /// <param name= "DT" >time to publish dynamic</param> /// <returns></returns> Public Static stringDatestringfromnow (DateTime DT) {TimeSpan span= DateTime.Now-DT; if(span. Totaldays > -) { returndt. ToShortDateString (); } Else if(span. Totaldays > -) { return "1 months ago"; } Else if(span. Totaldays > -) { return "2 weeks ago"; } Else if(span. Totaldays >7) { return "1 weeks ago"; } Else if(span. Totaldays >1) { return string. Format ("{0} days ago", (int) Math.floor (span. Totaldays)); } Else if(span. TotalHours >1) { return string. Format ("{0} hours ago", (int) Math.floor (span. TotalHours)); } Else if(span. Totalminutes >1) { return string. Format ("{0} minutes ago", (int) Math.floor (span. totalminutes)); } Else if(span. TotalSeconds >=1) { return string. Format ("{0} seconds ago", (int) Math.floor (span. TotalSeconds)); } Else { return "1 seconds ago"; } } #endregion
About C # get dynamic time difference function