Simulate the release time of WeChat moments and format the past time to xx (seconds/minute/hour/day ).

Source: Internet
Author: User

Simulate the release time of the circle of friends and format the past time to xx (seconds/minute/hour/day ).

Simulate the release time of the circle of friends, and use the extension method to display the past time as xx (seconds/minute/hour/day), take the message time in the message list as an example, let's take a look at the direct Time Display Effect (date. toString ("yyyy/MM/dd HH: mm: ss "))

I feel very general, nothing special

Next we will write an extension method to format the message time to xx (seconds/minute/hour/day) before

Decimal. Truncate (data) // retrieves the entire decimal number.

Public static class HtmlExpansion {// only formats the time in the past two days. public static string agdatefomat (this DateTime date) {var times = DateTime. now-date; var s = Convert. toDecimal (times. totalSeconds); // second var m = Convert. toDecimal (times. totalMinutes); // minute var h = Convert. toDecimal (times. totalHours); // hour var d = Convert. toDecimal (times. totalDays); // return s <60? "" + Decimal. Truncate (s) + "seconds ago": m <60? "" + Decimal. Truncate (m) + "Minutes Ago": h <24? "" + Decimal. Truncate (h) + "Hours Ago": d <2? "" + Decimal. Truncate (d) + "Days Ago": date. ToString ("yyyy/MM/dd HH: mm: ss ");}}

Call date. agdatefomat () directly after date. The result is as follows:

Does it feel a little higher than the previous time?

==== End ====

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.