Use C # To change the time of publication to a few years ago, months, days ago, hours ago, a few minutes ago, or a few seconds ago.

Source: Internet
Author: User
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"; }

        }

    }
}

 

Related Article

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.