• 将毫秒转化成时分秒


            //将毫秒转换成时分秒
            public string formatLongToTimeStr(long a)
            {
                try
                {
                    int hour = 0;
                    int minute = 0;
                    int second = 0;
    
                    second = Convert.ToInt32(a) / 1000;
    
                    if (second > 60)
                    {
                        minute = second / 60;
                        second = second % 60;
                    }
                    if (minute > 60)
                    {
                        hour = minute / 60;
                        minute = minute % 60;
                    }
                    var newHour = string.Empty;
                    if (Convert.ToString(hour).Length < 2)
                    {
                        newHour = "0" + Convert.ToString(hour);
                    }
                    else
                    {
                        newHour = Convert.ToString(hour);
                    }
                    var newMinute = string.Empty;
                    if (Convert.ToString(minute).Length < 2)
                    {
                        newMinute = "0" + Convert.ToString(minute);
                    }
                    else
                    {
                        newMinute = Convert.ToString(minute);
                    }
                    var newSecond = string.Empty;
                    if (Convert.ToString(second).Length < 2)
                    {
                        newSecond = "0" + Convert.ToString(second);
                    }
                    else
                    {
                        newSecond = Convert.ToString(second);
                    }
                    return (newHour + ":" + newMinute + ":"
                            + newSecond);
                }
                catch (Exception)
                {
                    return null;
                }
            }

    求取2个时间的差值

       #region 获取时间差string GetTime(BsonString getTime)
            /// <summary>
            /// 获取时间差
            /// </summary>
            /// <param name="getTime">数据库时间</param>
            /// <returns>时间差</returns>
            private string GetTime(BsonString getTime)
            {
                var time1 = DateTime.ParseExact(Convert.ToString(getTime), "yyyy-MM-dd HH:mm:ss", null);
                var time = DateTime.Now - time1;
    
                if (time.TotalHours > 24)
                {
                    return Math.Floor(time.TotalDays) + "天前";
                }
                if (time.TotalHours > 1)
                {
                    return Math.Floor(time.TotalHours) + "小时前";
                }
                if (time.TotalMinutes > 1)
                {
                    return Math.Floor(time.TotalMinutes) + "分钟前";
                }
                return Math.Floor(time.TotalSeconds) + "秒前";
            }
            #endregion
  • 相关阅读:
    docker笔记(1)
    解决MySQL8 #1227 – Access denied; you need (at least one of) the SYSTEM_USER privilege(s) for this ...
    nginx if判断&&和||写法
    泡泡后台Couchbase缓存使用经验分享
    ARTS-WEEK-007
    ARTS-WEEK-006
    ARTS-WEEK-005
    ARTS-WEEK-004
    ARTS-WEEK-003
    总结httpclient资源释放和连接复用
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/4331572.html
Copyright © 2020-2023  润新知