开发者

Need help with modifying a method to display date in informal way

开发者 https://www.devze.com 2023-03-31 17:14 出处:网络
The following method retuns null if the date islike few months back or year old. If I want to display date as \"2 month(s) ago\" or like \"1 year(s) ago\". How should I modify the following method?

The following method retuns null if the date is like few months back or year old. If I want to display date as "2 month(s) ago" or like "1 year(s) ago". How should I modify the following method?

 // 1.
        // Get time span elapsed since the date.
        TimeSpan s = DateTime.Now.Subtract(d);

        // 2.
        // Get total number of days elapsed.
        int dayDiff = (int)s.TotalDays;

        // 3.
        // Get total number of seconds elapsed.
        int secDiff = (int)s.TotalSeconds;

        // 4.
        // Don't allow out of range values.
        if (dayDiff < 0 || dayDiff >= 31)

        {
            return null;
        }

        // 5.
        // Handle same-day times.
        if (dayDiff == 0)
        {
            // A.
            // Less than one minute ago.
            if (secDiff < 60)
            {
                return "just now";
            }
            // B.
            // Less than 2 minutes ago.
            if (secDiff < 120)
            {
                return "1 minute ago";
            }
            // C.
            // Less than one hour ago.
            if (secDiff < 3600)
            {
                return string.Format("{0} minutes ago",
                    Math.Floor((double)secDiff / 60));
            }
            // D.
            // Less than 2 hours ago.
            if (secDiff < 7200)
            {
                return "1 hour ago";
            }
            // E.
            // Less than one day ago.
            if (secDiff < 86400)
            {
                return string.Format("{0} hours ago",
                    Math.Floor((double)secDiff / 3600));
            }
        }
        // 6.
        // Handle previous days.
        if (dayDiff == 1)
        {
            return "yesterday";
        }
        if (da开发者_StackOverflow社区yDiff < 7)
        {
            return string.Format("{0} days ago",
            dayDiff);
        }
        if (dayDiff < 31)
        {
            return string.Format("{0} weeks ago",
            Math.Ceiling((double)dayDiff / 7));
        }
        return null;
    }


After the if block: if (dayDiff < 31) { ... } insert the following code:

if (dayDiff < 365)
{
    return string.Format("{0} month(s) ago", Math.Ceiling((double)dayDiff / 31));
}
else
{
    return string.Format("{0} year(s) ago", Math.Ceiling((double)dayDiff / 365));
}


you can directly compare TimeSpans like:

TimeSpan s = DateTime.Now.Subtract(d);
if (s < TimeSpan.FromDays(1))
{
 // ...
}
else if (s < TimeSpan.FromMonth(1))
{
 // ...
}
// ...

just combine this the way you want exactly (sorry but that I cannot really parse from your question) I think you might like:

TimeSpan s = DateTime.Now.Subtract(d);
if (s < TimeSpan.FromDays(1))
{
 return string.Format("{0:0} hour(s) ago", s.TotalHours);
}
else if (s < TimeSpan.FromDays(7))
{
 return string.Format("{0:0} day(s) ago", s.TotalDays);
}
// ...
0

精彩评论

暂无评论...
验证码 换一张
取 消