代码如下
select group_date groupDate from (SELECT
@cdate input_date,
@last_sat := date_add(date_sub(date_sub(@cdate,interval day(@cdate)-1 day),interval 1 month), interval 6-date_format(date_sub(date_sub(@cdate,interval day(@cdate)-1 day),interval 1 month),'%w') day) last_sat,
@this_sat := date_add(date_sub(@cdate,interval day(@cdate)-1 day), interval 6-date_format(date_sub(@cdate,interval day(@cdate)-1 day),'%w') day) this_sat,
@next_sat := date_add(date_add(date_sub(@cdate,interval day(@cdate)-1 day),interval 1 month), interval 6-date_format(date_add(date_sub(@cdate,interval day(@cdate)-1 day),interval 1 month),'%w') day) next_sat,
@last_point := if(day(@last_sat)-3>0,date_sub(@last_sat,interval 7 day),@last_sat) last_point,
@this_point := if(day(@this_sat)-3>0,date_sub(@this_sat,interval 7 day),@this_sat) this_point,
@next_point := if(day(@next_sat)-3>0,date_sub(@next_sat,interval 7 day),@next_sat) next_point ,
case
when @cdate >= @last_point and @cdate < @this_point then
concat(
'W',
datediff(@cdate,@last_point) div 7 + 1, '/',
year(@cdate),'-',
if(month(@last_sat)>=10,month(@last_sat),concat('0',month(@last_sat)))
)
when @cdate >=@this_point and @cdate < @next_point then
concat(
'W',
datediff(@cdate,@this_point) div 7 + 1,'/',
year(@cdate),'-',
if(month(@this_sat)>=10,month(@this_sat),concat('0',month(@this_sat))))
else
concat(
'W',
datediff(@cdate,@next_point) div 7 + 1,'/',
year(@cdate),'-',if(month(@next_sat)>=10,month(@next_sat),concat('0',month(@next_sat))))
end
group_date
from (SELECT @cdate := #{inputDate}) as gd
);
<script>
function getInfo(year, month) {
var d = new Date();
// what day is first day
dsetFullYear(year, month-1, 1);
var w1 = dgetDay();
if (w1 == 0) w1 = 7;
// total day of month
dsetFullYear(year, month, 0);
var dd = dgetDate();
// first Monday
if (w1 != 1) d1 = 7 - w1 + 2;
else d1 = 1;
week_count = Mathceil((dd-d1+1)/7);
documentwrite(year + "年" + month + "月有" + week_count +"周<br/>");
for (var i = 0; i < week_count; i++) {
var monday = d1+i7;
var sunday = monday + 6;
var from = year+"/"+month+"/"+monday;
var to;
if (sunday <= dd) {
to = year+"/"+month+"/"+sunday;
} else {
dsetFullYear(year, month-1, sunday);
to = dgetFullYear()+"/"+(dgetMonth()+1)+"/"+dgetDate();
}
documentwrite("第"+(i+1)+"周 从" + from + " 到 " + to + "<br/>");
}
}
getInfo(2013,12);
</script>
创建函数
CREATE FUNCTION WeekOfMonth(@day datetime)RETURNS int
AS
begin
----declare @day datetime
declare @num int
declare @Start datetime
declare @dd int
declare @dayofweek char(8)
declare @dayofweek_num char(8)
declare @startWeekDays int
---set @day='2009-07-05'
if datepart(dd,@day)=1
return 1
else
set @Start= (SELECT DATEADD(mm, DATEDIFF(mm,0,@day), 0)) --一个月第一天的
set @dayofweek= (datename(weekday,@Start)) ---得到本月第一天是周几
set @dayofweek_num=(select (case @dayofweek when '星期一' then 2
when '星期二' then 3
when '星期三' then 4
when '星期四' then 5
when '星期五' then 6
when '星期六' then 7
when '星期日' then 1
end))
set @dayofweek_num= 7-@dayofweek_num+1 ---得到本月的第一周一共有几天
---print @dayofweek_num
set @dd=datepart(dd,@day) ----得到今天是这个月的第几天
--print @dd
if @dd<=@dayofweek_num --小于前一周的天数
return 1
else
set @dd=@dd-@dayofweek_num
if @dd % 7=0
begin
set @num=@dd / 7
return @num+1
end
else --if @dd % 7<>0
set @num=@dd / 7
set @num=@num+1+1
return @num
end
测试
select dboWeekOfMonth('2013-11-14')结果
Calendar c_begin = CalendargetInstance();
Calendar c_end = CalendargetInstance();
c_beginset(2010, 3, 1); //Calendar的月从0-11,所以4月是3
c_endset(2010, 4, 20); //Calendar的月从0-11,所以5月是4
int count = 0;
while(c_beginbefore(c_end)){
//Systemoutprintln(new javasqlDate(c_begingetTime()getTime()));
if(c_beginget(CalendarDAY_OF_WEEK)==CalendarMONDAY){
count++;
}
if(count == 2){
Systemoutprintln(c_beginget(CalendarYEAR)+"年"+(c_beginget(CalendarMONTH)+1)+"月"+c_beginget(CalendarDATE)+"日");
break;
}
c_beginadd(CalendarDAY_OF_YEAR, 1);
}
可以这样试试,应该能理解吧。如有不懂再问我
以上就是关于在mysql中得到当前日期是当月第几周全部的内容,包括:在mysql中得到当前日期是当月第几周、js 获取某年某月有几周,以及每周的周一和周末是几号到几号、判断某天是当月的第几周的sql函数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)