unity UTC时间差转换文本

/// <summary>
        /// UTC时间转换文本
        /// </summary>
        /// <param name="curTime">当前时间UTC</param>
        /// <param name="preTime">之前时间UTC</param>
        /// <param name="isHour">true -> 24h制;false -> 全日制</param>
        /// <returns></returns>
        public string setTimeValueText(int curTime, int preTime, bool isHour = true)
        {
    
    
            int valueTime = curTime - preTime;
            string timeText;
            if (isHour)
            {
    
    
                if (valueTime <= 180)
                {
    
    
                    timeText = "刚刚";
                }
                else if (valueTime <= 3600)
                {
    
    
                    timeText = string.Format("{0}分钟前", valueTime / 60);
                }
                else if (valueTime <= 86400)
                {
    
    
                    timeText = string.Format("{0}小时前", valueTime / 3600);
                }
                else if (valueTime <= 604800)
                {
    
    
                    timeText = string.Format("{0}天前", valueTime / 86400);
                }
                else
                {
    
    
                    timeText = "7天前";
                }
            }
            else
            {
    
    
                if (valueTime <= 180)
                {
    
    
                    timeText = "刚刚";
                }
                else if (valueTime <= 3600)
                {
    
    
                    timeText = string.Format("{0}分钟前", valueTime / 60);
                }
                else if (valueTime <= 86400)
                {
    
    
                    timeText = string.Format("{0}小时前", valueTime / 3600);
                }
                else
                {
    
    
                    long preTimeL = preTime;
                    System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
                    DateTime preTimeD = startTime.AddSeconds(preTimeL);
                    long curTimeL = curTime;
                    startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
                    DateTime curTimeD = startTime.AddSeconds(curTimeL);

                    int curTimeI = curTime - curTime % 86400;
                    int perTimeI = preTime - preTime % 86400;
                    int day = ((curTimeI - perTimeI) / 86400);
                    if (day == 1)
                    {
    
    
                        timeText = "昨天";
                    }
                    else if(day <= 7)
                    {
    
    
                        timeText = string.Format("{0}天前", day);
                    }
                    else
                    {
    
    
                        if(curTimeD.Year == preTimeD.Year)
                        {
    
    
                            timeText = string.Format("{0:MM-dd}", preTimeD);
                        }
                        else
                        {
    
    
                            timeText = string.Format("{0:yyyy-MM-dd}", preTimeD);
                        }
                    }
                }
            }

            return timeText;
        }

猜你喜欢

转载自blog.csdn.net/weixin_47819574/article/details/131292104