About C # getting the dynamic time difference function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.