如何计算日期..^^

cookies10wen 2009-06-02 11:44:17
用户选择一个开始日期和一个结束日期,, 并有七个复选框分别为星期一至星期日,,用户根据情况选择星期几,,如何好效计算出用户选择的天数????

比如:用户选择开始日期为6月1日,结束日期为7月15日,在复选框选择了星期一至星期五,,如何计算6月1日至7月15日星期一至星期五有多少天??
...全文
298 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
stromboy007 2009-06-02
  • 打赏
  • 举报
回复
哇塞 這么多高手。。。我都不好意思說了
nosuchtracter 2009-06-02
  • 打赏
  • 举报
回复
任意两个时间之间的星期几的次数-横.sql

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_weekdaycount]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_weekdaycount]
GO

/**//*--计算任意两个时间之间的星期几的次数(横向显示)

本方法直接判断 @@datefirst 做对应处理
不受 sp_language 及 set datefirst 的影响

--邹建 2004.08(引用请保留此信息)--*/

/**//*--调用示例

select * from f_weekdaycount('2004-9-01','2004-9-02')
--*/
create function f_weekdaycount(
@dt_begin datetime,
@dt_end datetime
)returns table
as
return(
select 跨周数
,周一=case a
when -1 then case when 1 between b and c then 1 else 0 end
when 0 then case when b<=1 then 1 else 0 end
+case when c>=1 then 1 else 0 end
else a+case when b<=1 then 1 else 0 end
+case when c>=1 then 1 else 0 end
end
,周二=case a
when -1 then case when 2 between b and c then 1 else 0 end
when 0 then case when b<=2 then 1 else 0 end
+case when c>=2 then 1 else 0 end
else a+case when b<=2 then 1 else 0 end
+case when c>=2 then 1 else 0 end
end
,周三=case a
when -1 then case when 3 between b and c then 1 else 0 end
when 0 then case when b<=3 then 1 else 0 end
+case when c>=3 then 1 else 0 end
else a+case when b<=3 then 1 else 0 end
+case when c>=3 then 1 else 0 end
end
,周四=case a
when -1 then case when 4 between b and c then 1 else 0 end
when 0 then case when b<=4 then 1 else 0 end
+case when c>=4 then 1 else 0 end
else a+case when b<=4 then 1 else 0 end
+case when c>=4 then 1 else 0 end
end
,周五=case a
when -1 then case when 5 between b and c then 1 else 0 end
when 0 then case when b<=5 then 1 else 0 end
+case when c>=5 then 1 else 0 end
else a+case when b<=5 then 1 else 0 end
+case when c>=5 then 1 else 0 end
end
,周六=case a
when -1 then case when 6 between b and c then 1 else 0 end
when 0 then case when b<=6 then 1 else 0 end
+case when c>=6 then 1 else 0 end
else a+case when b<=6 then 1 else 0 end
+case when c>=6 then 1 else 0 end
end
,周日=case a
when -1 then case when 0 between b and c then 1 else 0 end
when 0 then case when b<=0 then 1 else 0 end
+case when c>=0 then 1 else 0 end
else a+case when b<=0 then 1 else 0 end
+case when c>=0 then 1 else 0 end
end
from(
select 跨周数=case when @dt_begin<@dt_end
then (datediff(day,@dt_begin,@dt_end)+7)/7
else (datediff(day,@dt_end,@dt_begin)+7)/7 end
,a=case when @dt_begin<@dt_end
then datediff(week,@dt_begin,@dt_end)-1
else datediff(week,@dt_end,@dt_begin)-1 end
,b=case when @dt_begin<@dt_end
then (@@datefirst+datepart(weekday,@dt_begin)-1)%7
else (@@datefirst+datepart(weekday,@dt_end)-1)%7 end
,c=case when @dt_begin<@dt_end
then (@@datefirst+datepart(weekday,@dt_end)-1)%7
else (@@datefirst+datepart(weekday,@dt_begin)-1)%7 end)a
)
go
yechd 2009-06-02
  • 打赏
  • 举报
回复
/// <summary>
/// 获取时间段内星期几个天数综合
/// </summary>
/// <param name="beginDate">开始日期</param>
/// <param name="endDate">结束日期</param>
/// <param name="DayOfWeek">星期几</param>
/// <returns></returns>
private int GetDayCount(DateTime beginDate, DateTime endDate, int DayOfWeek)
{
int i = 0;

for (DateTime date = beginDate; date <= endDate; date = date.AddDays(1))
{
int day = (int)date.DayOfWeek;
if (day == DayOfWeek)
{
i++;
}
}

return i;
}
teerhu 2009-06-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cpp2017 的回复:]
DateTime dt1 = DateTime.Parse("2009-06-01");
DateTime dt2 = DateTime.Parse("2009-07-15");
int wd1 = (int)dt1.DayOfWeek;
int wd2 = (int)dt2.DayOfWeek;

int iDays =((TimeSpan) (dt2 - dt1)).Days;
int WorkDay = wd1;
iDays -= wd1;

if (iDays % 7 == 0)
{
WorkDay += (iDa…
[/Quote]
支持
wuyq11 2009-06-02
  • 打赏
  • 举报
回复

List<DateTime> lst=new List<DateTime>();
for (DateTime dt = DateTime.Parse("2009-6-1"); dt < DateTime.Parse("2009-7-15"); dt = dt.AddDays(1))
{
if(Convert.ToInt16(dt.DayOfWeek)>0 and Convert.ToInt16(dt.DayOfWeek)<6)
{
lst.Add(dt);
}
}

十八道胡同 2009-06-02
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Reflection;
using System.Collections;


namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i = 0;
for (DateTime dt = DateTime.Parse("2009-6-1"); dt < DateTime.Parse("2009-7-15"); dt = dt.AddDays(1))
{
string str = dt.DayOfWeek.ToString("D");
// Console.WriteLine(dt.DayOfWeek.ToString());
// Console.WriteLine(str);
if (str != "6" && str != "0")
{
i++;
}
}
Console.WriteLine(i);
Console.Read();

}


}
}



这个事对的
十八道胡同 2009-06-02
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Reflection;
using System.Collections;


namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i = 0;
for (DateTime dt = DateTime.Parse("2009-6-1"); dt < DateTime.Parse("2009-7-15"); dt = dt.AddDays(1))
{
string str = dt.DayOfWeek.ToString("D");
//Console.WriteLine(str);
if (str == "1" || str == "6")
{
i++;
}
}
Console.WriteLine(i);
Console.Read();

}


}
}

chengkouda 2009-06-02
  • 打赏
  • 举报
回复
学习了
liangweiwei130 2009-06-02
  • 打赏
  • 举报
回复

DateTime dt1 = Convert.ToDateTime(TextBox1.Text);
DateTime dt2 = Convert.ToDateTime(TextBox2.Text);
TimeSpan ts = dt2 - dt1;
Label1.Text = ts.Days.ToString();
int n = ts.Days;

switch (dt2.DayOfWeek.ToString())
{
case "Monday":
n = (n) / 7;
break;
case "Tuesday":
n = (n + 1) / 7;
break;
case "Wednesday":
n = (n + 2) / 7;
break;
case "Thursday":
n = (n + 3) / 7;
break;
case "Friday":
n = (n + 4) / 7;
break;
case "Saturday":
n = (n + 5) / 7;
break;
case "Sunday":
n = (n + 6) / 7;
break;
default:
n = 999;
break;
}
Label1.Text += "|"+n.ToString();
HDNGO 2009-06-02
  • 打赏
  • 举报
回复
DayOfWeek直接获得吧。。。


using System;
using System.Collections.Generic;
using System.Text;

namespace WeekCount
{
class Program
{
static void Main(string[] args)
{
int i = 0;
for (DateTime dt = DateTime.Parse("2009-6-17"); dt < DateTime.Parse("2009-7-17"); dt=dt.AddDays(1))
{
string str = dt.DayOfWeek.ToString("D");
if (str == "1" || str == "6")
{
i++;
}
}
Console.WriteLine(i);
Console.Read();
}
}
}
cpp2017 2009-06-02
  • 打赏
  • 举报
回复
DateTime dt1 = DateTime.Parse("2009-06-01");
DateTime dt2 = DateTime.Parse("2009-07-15");
int wd1 = (int)dt1.DayOfWeek;
int wd2 = (int)dt2.DayOfWeek;

int iDays =((TimeSpan) (dt2 - dt1)).Days;
int WorkDay = wd1;
iDays -= wd1;

if (iDays % 7 == 0)
{
WorkDay += (iDays / 7) * 5;
}
else
{
WorkDay += (iDays / 7) * 5 + wd2;
}
WorkDay +=1;

Response.Write(WorkDay.ToString());
lgaimin 2009-06-02
  • 打赏
  • 举报
回复
好像有这样的方法.我帮你找找.
HDNGO 2009-06-02
  • 打赏
  • 举报
回复
笨一点的就循环加吧~
CYANGRONG 2009-06-02
  • 打赏
  • 举报
回复
wangchao1982 2009-06-02
  • 打赏
  • 举报
回复
肯定不是最简单的,但是思路却是非常清晰的。

public virtual class DateTimeHelper
{
/// <summary>
/// 功能:用于统计从开始到结束时间段内的特定的DayOfWeek的天数
/// </summary>
/// <param name="paramStart">开始时间</param>
/// <param name="paramEnd">结束时间</param>
/// <param name="paramDayOfWeek">要统计的DayOfWeek</param>
/// <returns>返回从开始到结束时间段内的特定的DayOfWeek的天数</returns>
public static Int32 CalcualteDays(DateTime paramStart, DateTime paramEnd, params DayOfWeek[] paramDayOfWeek)
{
TimeSpan tempStartEndTimeSpan = new TimeSpan();
Int32 tempEffectiveDaysCount = 0;
Int32 tempTotalDayCount = 0;
int tempLeftDays = 0;
switch (paramDayOfWeek.Length)
{
case 0:
tempTotalDayCount = 0;
tempEffectiveDaysCount = 0;
break;
default:
if (paramEnd < paramStart)
{
DateTime? tempExchange = null;
tempExchange = paramEnd;
paramEnd = paramStart;
paramStart = (DateTime)tempExchange;
}
tempStartEndTimeSpan = paramEnd - paramStart;
tempTotalDayCount = tempStartEndTimeSpan.Days + 1;
if (tempTotalDayCount >= 7)
{
tempEffectiveDaysCount = (tempTotalDayCount / 7) * paramDayOfWeek.Length;
tempLeftDays = tempTotalDayCount % 7;
if (tempLeftDays > 0)
{
CalcualteDays(tempLeftDays, ref tempEffectiveDaysCount, paramStart, paramDayOfWeek);
}

}
else
{
tempLeftDays = tempTotalDayCount;
CalcualteDays(tempLeftDays, ref tempEffectiveDaysCount, paramStart, paramDayOfWeek);
}
break;
}

return tempEffectiveDaysCount;
}
/// <summary>
/// 用于统计剩余天数少于7天的特定的DayOfWeek的天数
/// </summary>
/// <param name="paramLeftDays">剩余的天数(少于7天)</param>
/// <param name="paramEffectiveDaysCount">用于累加的符合特定DayOfWeek的累加器</param>
/// <param name="paramStart">用于统计的开始日期</param>
/// <param name="paramDayOfWeek">特定的DayOfWeek</param>
private static void CalcualteDays(Int32 paramLeftDays, ref Int32 paramEffectiveDaysCount, DateTime paramStart, DayOfWeek[] paramDayOfWeek)
{
for (int i = 0; i < paramLeftDays; i++)
{
paramStart = paramStart.AddDays(i);
for (int j = 0; j < paramDayOfWeek.Length; j++)
{
if (paramDayOfWeek[j] == paramStart.DayOfWeek)
{
paramEffectiveDaysCount++;
}
}
}
}
}
suners 2009-06-02
  • 打赏
  • 举报
回复
感觉 怎么都挺合理的 不过还没应用到过 还是先学学
cookies10wen 2009-06-02
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 HDNGO 的回复:]
引用 17 楼 cookies10wen 的回复:
谢谢大家的解答,,但楼上的好像没完全实现到,,我说的只是比如,,但如果用户选择的不是星期一至星期五,,而是星期二至星期四呢???


汗。。方法都给你了。。具体的逻辑实现,貌似需要你自己写了吧。。。
[/Quote]


哦,,呵呵,,看了一下,,就你的比较合适..
zhaoqiliang527 2009-06-02
  • 打赏
  • 举报
回复
学习啦...
HDNGO 2009-06-02
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 cookies10wen 的回复:]
谢谢大家的解答,,但楼上的好像没完全实现到,,我说的只是比如,,但如果用户选择的不是星期一至星期五,,而是星期二至星期四呢???
[/Quote]

汗。。方法都给你了。。具体的逻辑实现,貌似需要你自己写了吧。。。
cookies10wen 2009-06-02
  • 打赏
  • 举报
回复
谢谢大家的解答,,但楼上的好像没完全实现到,,我说的只是比如,,但如果用户选择的不是星期一至星期五,,而是星期二至星期四呢???
加载更多回复(3)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧