zongzhibin
2024-11-27 5e610f4c9d9870b393720bc5fcc856e97bc2ea4b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace iWareCommon.Utils
{
    /// <summary>
    /// 日期处理类
    /// </summary>
    public class DateTimeHelper
    {
        /// <summary>
        /// 返回NULL
        /// </summary>
        /// <returns></returns>
        public static DateTime? GetNull()
        {
            return null;
        }
 
        /// <summary>
        /// 计算两个时间的时间差,返回秒数
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static int GetTimeDiffer(DateTime? startTime, DateTime? endTime, ref string msg)
        {
            if (startTime == null || endTime == null)
            {
                return 0;
            }
            TimeSpan ts = ((DateTime)endTime - (DateTime)startTime);
            msg = "";
            if (ts.Days != 0)
            {
                msg += ts.Days + "天";
            }
            if (ts.Hours != 0)
            {
                msg += ts.Hours + "小时";
            }
            if (ts.Minutes != 0)
            {
                msg += ts.Minutes + "分钟";
            }
            if (ts.Seconds != 0)
            {
                msg += ts.Seconds + "秒";
            }
            return (int)ts.TotalSeconds;
        }
 
        /// <summary>
        /// 转换日期格式为字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string ConvertToString(DateTime? dt)
        {
            if (dt == null)
            {
                return string.Empty;
            }
            return Convert.ToDateTime(dt).ToString("yyyy-MM-dd HH:mm:ss");
        }
 
        /// <summary>
        /// 转换日期格式为字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string ConvertToStringForOnlyShowTime(DateTime? dt)
        {
            if (dt == null)
            {
                return string.Empty;
            }
            return Convert.ToDateTime(dt).ToString("HH:mm:ss");
        }
    }
}