DateTimeUtil.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace MECF.Framework.Common.Utilities
  7. {
  8. public class DateTimeUtil
  9. {
  10. public static int GetSecond(string timeString)
  11. {
  12. if (string.IsNullOrEmpty(timeString)) return 0;
  13. if (TimeSpan.TryParse(timeString, out TimeSpan timeSpan))
  14. {
  15. // 获取总秒数(包括小数部分)
  16. double totalSeconds = timeSpan.TotalSeconds;
  17. // 将总秒数转换为整数秒数
  18. int integerSeconds = Convert.ToInt32(totalSeconds);
  19. return integerSeconds;
  20. }
  21. return 0;
  22. }
  23. public static int GetSecond(float number)
  24. {
  25. if (number.ToString().Contains('.'))
  26. {
  27. var strs = number.ToString().Split('.').Select(a => int.Parse(a)).ToList();
  28. return strs[0] * 3600 + strs[1] * 60;
  29. }
  30. else
  31. {
  32. return (int)number * 3600;
  33. }
  34. }
  35. public static string SecondToHHmmss(string value)
  36. {
  37. if (value == null)
  38. return "";
  39. if (float.TryParse(value, out float floatResult))
  40. {
  41. var secondAll = Math.Round(floatResult, 1, MidpointRounding.AwayFromZero);
  42. // 将总秒数转换为时间跨度
  43. TimeSpan timeSpan = TimeSpan.FromSeconds(secondAll);
  44. // 格式化为 HH:mm:ss
  45. string formattedTime = $"{timeSpan.Hours:D2}:{timeSpan.Minutes:D2}:{timeSpan.Seconds:D2}";
  46. return formattedTime;
  47. }
  48. return "00:00:00";
  49. }
  50. public static void ConvertSecondsToHoursAndMinutes(int totalSeconds, out int hours, out int minutes)
  51. {
  52. hours = totalSeconds / 3600;
  53. int remainingSeconds = totalSeconds % 3600;
  54. minutes = remainingSeconds / 60;
  55. }
  56. }
  57. }