在C#中复制moment.js fromNow()和toNow()

时间:2015-12-28 13:01:28

标签: c# razor momentjs

我试图提出这个问题,但有些用户认为这是重复的并且关闭了这个问题。我在评论中解释了它,但无论如何,我在C#中编写了一个扩展方法,它将出现在答案中。

1 个答案:

答案 0 :(得分:1)

这是从now()和toNow()复制moment.js的扩展。如果您的某些页面是服务器生成的(例如Razor视图引擎),并且您希望同时出现这种情况,那么它很有用。

public static class Extensions
{
    // For from_now and to_now
    static Dictionary<string, string> relative_time = new Dictionary<string, string>()
    {
        { "future" , "in {0}" },
        { "past" , "in {0}" },
        { "s" , "a few seconds ago" },
        { "m" , "a minute ago" },
        { "mm" , "{0} minutes ago" },
        { "h" , "an hour ago" },
        { "hh" , "{0} hours ago" },
        { "d" , "a day ago" },
        { "dd" , "{0} days ago" },
        { "M" , "a month ago" },
        { "MM" , "{0} months ago" },
        { "Y" , "a year ago" },
        { "YY" , "{0} years ago" },
        { "-s" , "in seconds" },
        { "-m" , "in a minute" },
        { "-mm" , "in {0} minutes" },
        { "-h" , "in an hour" },
        { "-hh" , "in {0} hours" },
        { "-d" , "in a day" },
        { "-dd" , "in {0} days" },
        { "-M" , "in a month" },
        { "-MM" , "in {0} months" },
        { "-Y" , "in a year" },
        { "-YY" , "in {0} years" },
    };

    /// <summary>
    /// replicate moment.js fromNow()
    /// </summary>
    /// <returns></returns>
    public static string from_now(this DateTime time_gmt)
    {
        // http://momentjs.com/docs/#/displaying/fromnow  : moment.js fromNow() doc

        var diff = DateTime.UtcNow - time_gmt;
        if (diff.TotalSeconds < 0)
        {
            return time_gmt.to_now();
        }
        else if (diff.TotalSeconds <= 45)
        {
            return relative_time["s"];
        }
        else if (diff.TotalSeconds <= 90)
        {
            return relative_time["m"];
        }
        else if (diff.TotalMinutes <= 45)
        {
            return string.Format(relative_time["mm"], Convert.ToInt32( diff.TotalMinutes));
        }
        else if (diff.TotalMinutes <= 90)
        {
            return relative_time["h"];
        }
        else if (diff.TotalHours <= 22)
        {
            return string.Format(relative_time["hh"], Convert.ToInt32(diff.TotalHours));
        }
        else if (diff.TotalHours <= 36)
        {
            return relative_time["d"];
        }
        else if (diff.TotalDays <= 25)
        {
            return string.Format(relative_time["dd"], Convert.ToInt32(diff.TotalDays));
        }
        else if (diff.TotalDays <= 45)
        {
            return relative_time["M"];
        }
        else if (diff.TotalDays <= 345)
        {
            return string.Format(relative_time["MM"], Convert.ToInt32(diff.TotalDays/(365.0/12.0)));
        }
        else if (diff.TotalDays <= 545)
        {
            return relative_time["Y"];
        }
        else if (diff.TotalDays > 545)
        {
            return string.Format(relative_time["YY"], Convert.ToInt32(diff.TotalDays/365.0));
        }
        return "invalid";
    }

    /// <summary>
    /// replicate moment.js toNow()
    /// </summary>
    /// <param name="time_gmt"></param>
    /// <returns></returns>
    public static string to_now(this DateTime time_gmt)
    {
        // http://momentjs.com/docs/#/displaying/tonow  : moment.js toNow() doc

        var diff = time_gmt - DateTime.UtcNow;
        if (diff.TotalSeconds < 0)
        {
            return time_gmt.from_now();
        }
        else if (diff.TotalSeconds <= 45)
        {
            return relative_time["-s"];
        }
        else if (diff.TotalSeconds <= 90)
        {
            return relative_time["-m"];
        }
        else if (diff.TotalMinutes <= 45)
        {
            return string.Format(relative_time["-mm"], Convert.ToInt32( diff.TotalMinutes));
        }
        else if (diff.TotalMinutes <= 90)
        {
            return relative_time["-h"];
        }
        else if (diff.TotalHours <= 22)
        {
            return string.Format(relative_time["-hh"], Convert.ToInt32(diff.TotalHours));
        }
        else if (diff.TotalHours <= 36)
        {
            return relative_time["-d"];
        }
        else if (diff.TotalDays <= 25)
        {
            return string.Format(relative_time["-dd"], Convert.ToInt32(diff.TotalDays));
        }
        else if (diff.TotalDays <= 45)
        {
            return relative_time["-M"];
        }
        else if (diff.TotalDays <= 345)
        {
            return string.Format(relative_time["-MM"], Convert.ToInt32(diff.TotalDays/(365.0/12.0)));
        }
        else if (diff.TotalDays <= 545)
        {
            return relative_time["-Y"];
        }
        else if (diff.TotalDays > 545)
        {
            return string.Format(relative_time["-YY"], Convert.ToInt32(diff.TotalDays/365.0));
        }
        return "invalid";
    }
}