|
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");
|
}
|
}
|
}
|