using System;
using System.Globalization;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace CRM_Support.BLL
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class DateTimeHelper
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
返回友好时间显示#region 返回友好时间显示
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
带时间#region 带时间
public static string GetManReadable(string datetime)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetManReadable(Convert.ToDateTime(datetime));
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return datetime;
}
}
public static string GetManReadable(object datetime)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetManReadable(Convert.ToDateTime(datetime));
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return datetime.ToString();
}
}
public static string GetManReadable(DateTime datetime)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string time = datetime.ToShortTimeString();
return GetShortManReadable(datetime) + " " + time;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
不带时间#region 不带时间
public static string GetShortManReadable(string datetime)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (string.IsNullOrEmpty(datetime.Trim()))
return string.Empty;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetShortManReadable(Convert.ToDateTime(datetime));
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return datetime;
}
}
public static string GetShortManReadable(object datetime)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (datetime == null || datetime.ToString().Trim() == string.Empty)
return string.Empty;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetShortManReadable(Convert.ToDateTime(datetime));
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return datetime.ToString();
}
}
public static string GetShortManReadable(DateTime datetime)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DateTime now = DateTime.Now;
if (now.Year == datetime.Year)//以下的前提是两时间都为同一年
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TimeSpan span = now.Date - datetime.Date;
int days = span.Days;
switch (days)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 1:
return "昨天";
case 0:
return "今天";
case -1:
return "明天";
default:
break;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (days >= -14 || days <= 14)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GregorianCalendar gc = new GregorianCalendar();
int dateWeekofYear = gc.GetWeekOfYear(datetime, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
int nowWeekofYear = gc.GetWeekOfYear(now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
string dateDayofWeek = gc.GetDayOfWeek(datetime).ToString();
int weeks = nowWeekofYear - dateWeekofYear;
switch (weeks)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 1:
return string.Format("上周{0}", WhichDay(dateDayofWeek));
case 0:
return string.Format("本周{0}", WhichDay(dateDayofWeek));
case -1:
return string.Format("下周{0}", WhichDay(dateDayofWeek));
default:
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (days >= -62 || days <= 62)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int months = now.Month - datetime.Month;
int dayofMonth = datetime.Day;
switch (months)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 1:
return string.Format("上月{0}号", dayofMonth);
case 0:
return string.Format("本月{0}号", dayofMonth);
case -1:
return string.Format("下月{0}号", dayofMonth);
default:
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
else//以下的前提是两时间不同年
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
return datetime.ToShortDateString();
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static string WhichDay(string enWeek)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (enWeek.Trim())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case "Sunday":
return "日";
case "Monday":
return "一";
case "Tuesday":
return "二";
case "Wednesday":
return "三";
case "Thursday":
return "四";
case "Friday":
return "五";
case "Saturday":
return "六";
default:
return enWeek;
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
生日提醒#region 生日提醒
public static string GetBirthdayTip(DateTime birthday)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DateTime now = DateTime.Now;
//TimeSpan span = DateTime.Now - birthday;
int nowMonth = now.Month;
int birtMonth = birthday.Month;
if (nowMonth == 12 && birtMonth == 1)
return string.Format("下月{0}号", birthday.Day);
if (nowMonth == 1 && birtMonth == 12)
return string.Format("上月{0}号", birthday.Day);
int months = now.Month - birthday.Month;
//int days = now.Day - birthday.Day;
if (months == 1)
return string.Format("上月{0}号", birthday.Day);
else if (months == -1)
return string.Format("下月{0}号", birthday.Day);
else if (months == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (now.Day == birthday.Day)
return "今天";
return string.Format("本月{0}号", birthday.Day);
}
else
return birthday.ToShortDateString();
}
public static string GetBirthdayTip(string birthday)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetBirthdayTip(Convert.ToDateTime(birthday));
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return birthday;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
其他日期相关#region 其他日期相关
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回日期加短时间格式
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string GetDateShortTime(DateTime dt)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return dt.ToString("yyyy-MM-dd hh:mm");
}
public static string GetDateShortTime(object o1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetDateShortTime(Convert.ToDateTime(o1));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return o1.ToString();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回短日期
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string GetShortDate(DateTime dt)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return dt.ToString("yyyy-MM-dd");
}
public static string GetShortDate(object o1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetShortDate(Convert.ToDateTime(o1));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return o1.ToString();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取下个月是几月
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static int GetPreviousMonth(DateTime date)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return date.AddMonths(-1).Month;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取当月是几月
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static int GetThisMonth(DateTime date)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return date.Month;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取下个月是几月
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static int GetNextMonth(DateTime date)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return date.AddMonths(1).Month;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取前或后几个月是几月
/// </summary>
/// <param name="i"></param>
/// <param name="date"></param>
/// <param name="year"></param>
/// <returns></returns>
public static int GetMonth(int i, DateTime date, out int year)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DateTime time = date.AddMonths(i);
year = time.Year;
return time.Month;
}
public static string GetWeek()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return string.Empty;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted on
2008-06-23 11:11
上午的绝缘杯
阅读(
639)
评论()
编辑
收藏
举报