1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace BLL
6{
7 /**//// <summary>
8 /// 时间帮助工具类
9 /// </summary>
10 class DateTimeUtils
11 {
12
13 根据时间返回几个月前,几天前,几小时前,几分钟前,几秒前#region 根据时间返回几个月前,几天前,几小时前,几分钟前,几秒前
14
15 /**//// <summary>
16 /// 根据时间返回几个月前,几天前,几小时前,几分钟前,几秒前
17 /// </summary>
18 /// <param name="dt"></param>
19 /// <returns></returns>
20 public static string DateStringFromNow(DateTime dt)
21 {
22 TimeSpan span = DateTime.Now - dt;
23 if (span.TotalDays > 60)
24 {
25 return dt.ToShortDateString();
26 }
27 else if (span.TotalDays > 30)
28 {
29 return "1个月前";
30 }
31 else if (span.TotalDays > 14)
32 {
33 return "2周前";
34 }
35 else if (span.TotalDays > 7)
36 {
37 return "1周前";
38 }
39 else if (span.TotalDays > 1)
40 {
41 return string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
42 }
43 else if (span.TotalHours > 1)
44 {
45 return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
46 }
47 else if (span.TotalMinutes > 1)
48 {
49 return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
50 }
51 else if (span.TotalSeconds >= 1)
52 {
53 return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
54 }
55 else
56 {
57 return "1秒前";
58 }
59 }
60
61 #endregion
62
63 时间差计算#region 时间差计算
64
65 /**//// <summary>
66 /// 计算两个时间的差值,返回的是x天x小时x分钟x秒
67 /// </summary>
68 /// <param name="DateTime1"></param>
69 /// <param name="DateTime2"></param>
70 /// <returns></returns>
71 public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
72 {
73 string dateDiff = null;
74 TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
75 TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
76 TimeSpan ts = ts1.Subtract(ts2).Duration();
77 //TimeSpan ts=ts1.Add(ts2).Duration();
78 dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
79 return dateDiff;
80 }
81
82 /**//// <summary>
83 /// 时间相差值,返回时间差
84 /// 调用时,isTotal为true时,返回的时带小数的天数,否则返回的是整数
85 /// </summary>
86 /// <param name="DateTime1"></param>
87 /// <param name="DateTime2"></param>
88 /// <param name="isTotal"></param>
89 /// <returns></returns>
90 public static string DateDiff(DateTime DateTime1, DateTime DateTime2, bool isTotal)
91 {
92 TimeSpan ts = DateTime2 - DateTime1;
93 if (isTotal)
94 //带小数的天数,比如1天12小时结果就是1.5
95 return ts.TotalDays.ToString();
96 else
97 //整数天数,1天12小时或者1天20小时结果都是1
98 return ts.Days.ToString();
99 }
100
101 #endregion
102
103 日期比较#region 日期比较
104
105 /**//// <summary>
106 /// 日期比较
107 /// </summary>
108 /// <param name="today">当前日期</param>
109 /// <param name="writeDate">输入日期</param>
110 /// <param name="n">比较天数</param>
111 /// <returns>大于天数返回true,小于返回false</returns>
112 public static bool CompareDate(string today, string writeDate, int n)
113 {
114 DateTime Today = Convert.ToDateTime(today);
115 DateTime WriteDate = Convert.ToDateTime(writeDate);
116 WriteDate = WriteDate.AddDays(n);
117 if (Today >= WriteDate)
118 return false;
119 else
120 return true;
121 }
122
123 #endregion
124
125 根据英文的星期几返回中文的星期几#region 根据英文的星期几返回中文的星期几
126
127 /**//// <summary>
128 /// 根据英文的星期几返回中文的星期几
129 /// 如WhichDay("Sunday"),返回星期日
130 /// </summary>
131 /// <param name="enWeek"></param>
132 /// <returns></returns>
133 public static string WhichDay(string enWeek)
134 {
135 switch (enWeek.Trim())
136 {
137 case "Sunday":
138 return "星期日";
139 case "Monday":
140 return "星期一";
141 case "Tuesday":
142 return "星期二";
143 case "Wednesday":
144 return "星期三";
145 case "Thursday":
146 return "星期四";
147 case "Friday":
148 return "星期五";
149 case "Saturday":
150 return "星期六";
151 default:
152 return enWeek;
153 }
154 }
155
156 #endregion
157
158 生日提醒#region 生日提醒
159
160 /**//// <summary>
161 /// 根据出生年月进行生日提醒
162 /// </summary>
163 /// <param name="birthday"></param>
164 /// <returns></returns>
165 public static string GetBirthdayTip(DateTime birthday)
166 {
167 DateTime now = DateTime.Now;
168 //TimeSpan span = DateTime.Now - birthday;
169 int nowMonth = now.Month;
170 int birtMonth = birthday.Month;
171 if (nowMonth == 12 && birtMonth == 1)
172 return string.Format("下月{0}号", birthday.Day);
173 if (nowMonth == 1 && birtMonth == 12)
174 return string.Format("上月{0}号", birthday.Day);
175 int months = now.Month - birthday.Month;
176 //int days = now.Day - birthday.Day;
177 if (months == 1)
178 return string.Format("上月{0}号", birthday.Day);
179 else if (months == -1)
180 return string.Format("下月{0}号", birthday.Day);
181 else if (months == 0)
182 {
183 if (now.Day == birthday.Day)
184 return "今天";
185 return string.Format("本月{0}号", birthday.Day);
186 }
187 else if (months > 1)
188 return string.Format("已过{0}月", months);
189 else
190 return string.Format("{0}月{1}日", birthday.Month, birthday.Day);
191 }
192
193 #endregion
194
195 }
196}
197
posted on 2009-03-13 15:33
aisoft 阅读(918)
评论(0) 编辑 收藏 所属分类:
.NET技术