//将毫秒转换成时分秒 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
时间: 2024-10-02 05:23:59