C# 时间处理[转载]
2006-06-29 17:05
435 查看
一、取某月的最后一天
法一、使用算出该月多少天,年+月+加上多少天即得,举例取今天这个月的最后一天
private void GetLastDateForMonth(DateTime DtStart,out DateTime DtEnd)
{
int Dtyear,DtMonth;
DtStart = DateTime.Now;
Dtyear = DtStart.Year;
DtMonth = DtStart.Month;
int MonthCount = DateTime.DaysInMonth(Dtyear,DtMonth);
DtEnd = Convert.ToDateTime(Dtyear.ToString()+"-"+DtMonth.ToString()+"-"+MonthCount);
}
法二、取出下月的第一天减去一天便是这个的最后一天
private void GetLastDateForMonth(DateTime DtStart,out DateTime DtEnd)
{
int Dtyear,DtMonth;
DtStart = DateTime.Now.AddMonths(1);
Dtyear = DtStart.Year;
DtMonth = DtStart.Month;
DtEnd = Convert.ToDateTime(Dtyear.ToString()+"-"+DtMonth.ToString()+"-"+"1").AddDays(-1);
}
二、时间差的计算
法一、使用TimeSpan ,同时也介绍一下TimeSpan的用法
相关属性和函数
Add:与另一个TimeSpan值相加。
Days:返回用天数计算的TimeSpan值。
Duration:获取TimeSpan的绝对值。
Hours:返回用小时计算的TimeSpan值
Milliseconds:返回用毫秒计算的TimeSpan值。
Minutes:返回用分钟计算的TimeSpan值。
Negate:返回当前实例的相反数。
Seconds:返回用秒计算的TimeSpan值。
Subtract:从中减去另一个TimeSpan值。
Ticks:返回TimeSpan值的tick数。
TotalDays:返回TimeSpan值表示的天数。
TotalHours:返回TimeSpan值表示的小时数。
TotalMilliseconds:返回TimeSpan值表示的毫秒数。
TotalMinutes:返回TimeSpan值表示的分钟数。
TotalSeconds:返回TimeSpan值表示的秒数。
简单示例:
DateTime d1 =new DateTime(2004,1,1,15,36,05);
DateTime d2 =new DateTime(2004,3,1,20,16,35);
TimeSpan d3 = d2.Subtract(d1);
LbTime.Text = "相差:"
+d3.Days.ToString()+"天"
+d3.Hours.ToString()+"小时"
+d3.Minutes.ToString()+"分钟"
+d3.Seconds.ToString()+"秒";
法二、使用Sql中的DATEDIFF函数
使用方法:DATEDIFF ( datepart , startdate , enddate )
它能帮你取出你想要的各种形式的时间差,如相隔多少天,多少小时,多少分钟等,具体格式如下:
如:datediff(mi,DtOpTime,DtEnd) 便能取出他们之间时间差的分钟总数,已经帮你换算好了,对于要求规定单位,时、分、秒特别有用
三 计算两个日期之间相差的工作日天数(转载:http://wrfwjn.blog.hexun.com/482517_d.html)
/// <summary>
/// 计算两个日期之间相差的工作日天数
/// </summary>
/// <param name="dtStart">开始日期</param>
/// <param name="dtEnd">结束日期</param>
/// <returns>Int</returns>
public int CalculateWorkingDays(DateTime dtStart, DateTime dtEnd)
{
int count=0;
for(DateTime dtTemp=dtStart;dtTemp<dtEnd;dTemp=dtTemp.AddDays(1))
{
// if(dtTemp.DayOfWeek!=DayOfWeek.Saturday&&dtTemp.DayOfWeek!=DayOfWeek.Sunday)
// {
// count++;
// }
count++;
}
return count;
}
法一、使用算出该月多少天,年+月+加上多少天即得,举例取今天这个月的最后一天
private void GetLastDateForMonth(DateTime DtStart,out DateTime DtEnd)
{
int Dtyear,DtMonth;
DtStart = DateTime.Now;
Dtyear = DtStart.Year;
DtMonth = DtStart.Month;
int MonthCount = DateTime.DaysInMonth(Dtyear,DtMonth);
DtEnd = Convert.ToDateTime(Dtyear.ToString()+"-"+DtMonth.ToString()+"-"+MonthCount);
}
法二、取出下月的第一天减去一天便是这个的最后一天
private void GetLastDateForMonth(DateTime DtStart,out DateTime DtEnd)
{
int Dtyear,DtMonth;
DtStart = DateTime.Now.AddMonths(1);
Dtyear = DtStart.Year;
DtMonth = DtStart.Month;
DtEnd = Convert.ToDateTime(Dtyear.ToString()+"-"+DtMonth.ToString()+"-"+"1").AddDays(-1);
}
二、时间差的计算
法一、使用TimeSpan ,同时也介绍一下TimeSpan的用法
相关属性和函数
Add:与另一个TimeSpan值相加。
Days:返回用天数计算的TimeSpan值。
Duration:获取TimeSpan的绝对值。
Hours:返回用小时计算的TimeSpan值
Milliseconds:返回用毫秒计算的TimeSpan值。
Minutes:返回用分钟计算的TimeSpan值。
Negate:返回当前实例的相反数。
Seconds:返回用秒计算的TimeSpan值。
Subtract:从中减去另一个TimeSpan值。
Ticks:返回TimeSpan值的tick数。
TotalDays:返回TimeSpan值表示的天数。
TotalHours:返回TimeSpan值表示的小时数。
TotalMilliseconds:返回TimeSpan值表示的毫秒数。
TotalMinutes:返回TimeSpan值表示的分钟数。
TotalSeconds:返回TimeSpan值表示的秒数。
简单示例:
DateTime d1 =new DateTime(2004,1,1,15,36,05);
DateTime d2 =new DateTime(2004,3,1,20,16,35);
TimeSpan d3 = d2.Subtract(d1);
LbTime.Text = "相差:"
+d3.Days.ToString()+"天"
+d3.Hours.ToString()+"小时"
+d3.Minutes.ToString()+"分钟"
+d3.Seconds.ToString()+"秒";
法二、使用Sql中的DATEDIFF函数
使用方法:DATEDIFF ( datepart , startdate , enddate )
它能帮你取出你想要的各种形式的时间差,如相隔多少天,多少小时,多少分钟等,具体格式如下:
日期部分 | 缩写 |
---|---|
year | yy, yyyy |
quarter | qq, q |
Month | mm, m |
dayofyear | dy, y |
Day | dd, d |
Week | wk, ww |
Hour | hh |
minute | mi, n |
second | ss, s |
millisecond | ms |
三 计算两个日期之间相差的工作日天数(转载:http://wrfwjn.blog.hexun.com/482517_d.html)
/// <summary>
/// 计算两个日期之间相差的工作日天数
/// </summary>
/// <param name="dtStart">开始日期</param>
/// <param name="dtEnd">结束日期</param>
/// <returns>Int</returns>
public int CalculateWorkingDays(DateTime dtStart, DateTime dtEnd)
{
int count=0;
for(DateTime dtTemp=dtStart;dtTemp<dtEnd;dTemp=dtTemp.AddDays(1))
{
// if(dtTemp.DayOfWeek!=DayOfWeek.Saturday&&dtTemp.DayOfWeek!=DayOfWeek.Sunday)
// {
// count++;
// }
count++;
}
return count;
}
相关文章推荐
- C# 时间处理[转载]
- [转]c# + mysql + 事务处理(转载于 《C#数据库事务原理及实践》)
- 时间与日期处理【转载】
- 浅析C#的事件处理和自定义事件 (转载)
- C#时间处理函数
- [时间操作] C#TimeHelper时间格式化帮助类 (转载)
- 请帮助我写个批处理 比较两个文件的修改时间 (转载)
- C#处理和计算微博发布时间
- <转载>在C#中利用Keep-Alive处理Socket网络异常断开的方法
- C#处理时间--显示发表时间已过多久
- c#对时间的处理(待添加)
- 【转载】QPS,用户平均等待时间,服务器平均请求处理时间
- C#与网络时间同步的时间处理函数代码
- javascript处理时间的函数【转载】
- 解决c#所有单线程单元(STA)线程都应使用泵式等待基元(如 CoWaitForMultipleHandles),并在运行时间很长的操作过程中定期发送消息。 转载
- 弱鸡3,C# 一个Timer在等待过程中,interval被改变,时间如何处理,
- C#处理Excel的读写的机制(转载)
- C#时间格式大全 转载的
- c# 处理时间
- C#有关时间的处理