根据年月周或者日期范围的问题
我想根据选择的年月周,来获取时间范围。比如:2013年,7月,二周.那么就是2013-07-08 到 2013-07-14 等
请问怎么计算呀??? --------------------编程问答-------------------- 自己拼接…… --------------------编程问答-------------------- 自己设计解决,如输入日期求月中第几周周几蛇年马月猪日蛇时 --------------------编程问答-------------------- create table A(dateAdded datetime)
declare @StartTime datetime='2013-07-01',
@EndTIme datetime ='2013-08-01'
while @StartTime<=@EndTIme
begin
insert into A(dateAdded) select @StartTime
set @StartTime=DATEADD(day,1,@StartTime)
end
/*DateAdded 这个字段是表中对应的字段*/
select
CASE WHEN DateAdd(day,datediff(day,@StartTime,a.DateAdded)/7*7+6,@StartTime)> @EndTime
THEN CONVERT(varchar(100), DateAdd(day,datediff(day,@StartTime,a.DateAdded)/7*7,@StartTime), 23)
+'~'+CONVERT(varchar(100), @EndTime, 23)
ELSE CONVERT(varchar(100), DateAdd(day,datediff(day,@StartTime,a.DateAdded)/7*7,@StartTime), 23)
+'~'+CONVERT(varchar(100), DateAdd(day,datediff(day,@StartTime,a.DateAdded)/7*7+6,@StartTime), 23)
END AS WeekZone
from A as a --------------------编程问答-------------------- 手写的,自己看看哪里需要修改
//当月的天数
protected int solarDays(int y, int m)
{
int[] solarMonth = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (m == 2)
return (((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0)) ? 29 : 28);
else
return (solarMonth[m - 1]);
}
protected void Button1_Click(object sender, EventArgs e)
{
var year = 2013;
var month = 2;
var week = 2;
int c;
c = solarDays(year, month);
DateTime start = new DateTime(year, month, week > 1 ? (week - 1) * 7 + 1 : 1);
DateTime end;
if (start.Day <= c)
{
end = start.AddDays(6);
}
else
{
end = new DateTime(year, month, c);
}
} --------------------编程问答-------------------- 参考:http://bbs.csdn.net/topics/390361320 --------------------编程问答-------------------- 2013年,7月,二周
就是得到当前月份的一天,看他是不是星期1
如果是,第二周起始就是当前+7结束就是+14 --------------------编程问答--------------------
create function fn_getwk(
@year int,--输入显示的年份
@month int, --输入显示的月份
@type bit=0 --指定每周的第一天,默认为星期日(中国习惯),如果参数为1就是星期一为每周的第一天
) returns @t table(
周数 nvarchar(10),
开始日期 varchar(10),
结束日期 varchar(10)
) as
begin
declare @d datetime
set @d=dateadd(wk,datediff(wk,'1900',cast(ltrim(@year*10000+@month*100+1) as datetime)),'1900')+@type
;with t as
(
select top (datediff(dd,ltrim(@year*10000+@month*100+1),ltrim(@year*10000+(@month+1)*100+1)))
[date]=cast(ltrim(@year*10000+@month*100+1) as datetime)-1
+row_number()over(order by getdate())
from sysobjects
)
insert @t
select
case
when [date] between @d and @d+5 then '第一周'
when [date] between @d+6 and @d+12 then '第二周'
when [date] between @d+13 and @d+19 then '第三周'
when [date] between @d+20 and @d+26 then '第四周'
when [date] between @d+27 and @d+33 then '第五周'
else '第六周'
end AS 周数,
convert(varchar,min([date]),23) 开始日期,
convert(varchar,max([date]),23) 结束日期
from t
group by
case
when [date] between @d and @d+5 then '第一周'
when [date] between @d+6 and @d+12 then '第二周'
when [date] between @d+13 and @d+19 then '第三周'
when [date] between @d+20 and @d+26 then '第四周'
when [date] between @d+27 and @d+33 then '第五周'
else '第六周'
end
order by 2
return
end
GO
select * from fn_getwk(2011,3,0)
/*
周数 开始日期 结束日期
---------- ---------- ----------
第一周 2010-05-01 2010-05-01
第二周 2010-05-02 2010-05-08
第三周 2010-05-09 2010-05-15
第四周 2010-05-16 2010-05-22
第五周 2010-05-23 2010-05-29
第六周 2010-05-30 2010-05-31
(6 行受影响)
*/
select * from fn_getwk(2010,5,1)
/*
周数 开始日期 结束日期
---------- ---------- ----------
第一周 2010-05-01 2010-05-02
第二周 2010-05-03 2010-05-09
第三周 2010-05-10 2010-05-16
第四周 2010-05-17 2010-05-23
第五周 2010-05-24 2010-05-30
第六周 2010-05-31 2010-05-31
*/
补充:.NET技术 , ASP.NET