zoukankan      html  css  js  c++  java
  • 转换时间为 “XX分钟之前”

    public static string getTimeAgo(string strDate)
        {
            string strTime = string.Empty;
            if (clsCommon.IsDate(strDate))
            {
                TimeSpan t = DateTime.UtcNow - Convert.ToDateTime(strDate);
                double deltaSeconds = t.TotalSeconds;
    
                double deltaMinutes = deltaSeconds / 60.0f;
                int minutes;
    
                if (deltaSeconds < 5)
                {
                    return "当前";
                }
                else if (deltaSeconds < 60)
                {
                    return Math.Floor(deltaSeconds) + " 秒之前";
                }
                else if (deltaSeconds < 120)
                {
                    return "分钟之前";
                }
                else if (deltaMinutes < 60)
                {
                    return Math.Floor(deltaMinutes) + " minutes ago";
                }
                else if (deltaMinutes < 120)
                {
                    return "An hour ago";
                }
                else if (deltaMinutes < (24 * 60))
                {
                    minutes = (int)Math.Floor(deltaMinutes / 60);
                    return minutes + " hours ago";
                }
                else if (deltaMinutes < (24 * 60 * 2))
                {
                    return "Yesterday";
                }
                else if (deltaMinutes < (24 * 60 * 7))
                {
                    minutes = (int)Math.Floor(deltaMinutes / (60 * 24));
                    return minutes + " days ago";
                }
                else if (deltaMinutes < (24 * 60 * 14))
                {
                    return "Last week";
                }
                else if (deltaMinutes < (24 * 60 * 31))
                {
                    minutes = (int)Math.Floor(deltaMinutes / (60 * 24 * 7));
                    return minutes + " weeks ago";
                }
                else if (deltaMinutes < (24 * 60 * 61))
                {
                    return "Last month";
                }
                else if (deltaMinutes < (24 * 60 * 365.25))
                {
                    minutes = (int)Math.Floor(deltaMinutes / (60 * 24 * 30));
                    return minutes + " months ago";
                }
                else if (deltaMinutes < (24 * 60 * 731))
                {
                    return "Last year";
                }
    
                minutes = (int)Math.Floor(deltaMinutes / (60 * 24 * 365));
                return minutes + " years ago";
            }
            else
            {
                return "";
            }
        }
    调用方法:
    getTimeAgo(DateTime.Now.ToString());
  • 相关阅读:
    Windows如何使用jstack跟踪异常代码
    内存溢出和内存泄漏的区别
    Tomcat 服务器性能优化
    Spotlight on oracle
    JVM性能调优监控工具jps、jstack、jmap、jhat、jstat使用详解(转VIII)
    linux下使用yum安装mysql
    MySQL监控系统MySQL MTOP的搭建(转VIII)
    SQL中Group By的使用
    视图
    触发器
  • 原文地址:https://www.cnblogs.com/zeroone/p/3737293.html
Copyright © 2011-2022 走看看