关于获取时间后,时间格式为几天前,几小时前格式转化
public string funString_DateNear(int intDays, string strDay)
{
DateTime? dt = null;
string strTmp = strDay;
int intH = 0;
bool isNear = false;
if (strTmp.IndexOf("小时前") >= 0)
{
int.TryParse(strTmp.Replace("小时前", "").Trim(), out intH);
dt = DateTime.Now.AddHours(-intH);
isNear = true;
}
if (strTmp.IndexOf("天前") >= 0)
{
int.TryParse(strTmp.Replace("天前", "").Trim(), out intH);
dt = DateTime.Now.AddHours(-intH);
isNear = true;
}
if (strTmp.IndexOf("分钟前") >= 0)
{
int.TryParse(strTmp.Replace("分钟前", "").Trim(), out intH);
dt = DateTime.Now.AddHours(-intH);
isNear = true;
}
if (!isNear)
{
dt = funDate_StringToDatetime(strDay);
}
if (dt == null)
{
return "";
}
if (((DateTime)dt) > DateTime.Now.AddDays(-intDays))
{
return ((DateTime)dt).ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
return "";
}
}
public DateTime? funDate_StringToDatetime(string strValue)
{
if (Microsoft.VisualBasic.Information.IsDate(strValue))
{
return DateTime.Parse(strValue);
}
else
{
return null;
}
}