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

本文关键字:toNow jsfromNow moment 复制 | 更新日期: 2023-09-27 18:28:19

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

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

这里是用于复制moment.js from Now()和to Now(()的扩展。如果你的一些页面是服务器生成的(例如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";
    }
}