C#中用当前日期算周的若干问题?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#中用当前日期算周的若干问题?相关的知识,希望对你有一定的参考价值。
问题1:
知道当前日期算是星期几?
一定要正确啊!
问题2:
算当前日期属于第几周?
规则:若是每月的1号是周一,二,三,四,则:该1号是该月的第一周,
若是每月的1号是周五,周六,周日,则:该月的第一周从4号开始。
我也想该怎么写,先与大家分享一下问题!
Q2:private int getWeeksOfDayInMonth(DateTime dt)
DateTime monthFirstDay = DateTime.Parse(dt.Year.ToString() + "-" + dt.Month.ToString() + "-1");
int startDay = 1;//第一周从哪天开始
int dayOfOneWeek = 0;//第一周有几天
switch (monthFirstDay.DayOfWeek)
case DayOfWeek.Monday: dayOfOneWeek = 7; startDay = 1;
break;
case DayOfWeek.Tuesday: dayOfOneWeek = 6; startDay = 1;
break;
case DayOfWeek.Wednesday: dayOfOneWeek = 5; startDay = 1;
break;
case DayOfWeek.Thursday: dayOfOneWeek = 4; startDay = 1;
break;
case DayOfWeek.Friday: dayOfOneWeek = 7; startDay = 4;
break;
case DayOfWeek.Saturday: dayOfOneWeek = 7; startDay = 3;
break;
case DayOfWeek.Sunday: dayOfOneWeek = 7; startDay = 2;
break;
int returnValue = 0;
int span = dt.Day - startDay;
if (span < 0)
returnValue = 0;
else
returnValue = span < dayOfOneWeek ? 1 : ((span - dayOfOneWeek) / 7 + 2);
return returnValue;
参考技术A DateTime.Now.DayOfWeek
第二个问题的话你就要加上自己的判断逻辑了!
layui日期选择框实现选择当前周的日期范围
直接上代码:
layui.use(['laydate'], function()
var laydate = layui.laydate;
var monday = getMonday(new Date());
var mm = layui.util.toDateString(monday,'yyyy-MM-dd')
var sunday = GetDateStr(monday,6);
laydate.render(
elem: '#daterange'//绑定的html元素id
,type:'date'
,format: 'yyyy-MM-dd' //格式
,trigger : 'click'
,min:mm //最小可选择日期
,max:sunday //最大可选择日期
,range:true //设置启用日期范围
);
)
//获取当前日期的周一日期
function getMonday( date )
var day = date.getDay() || 7;
if( day !== 1 )
date.setHours(-24 * (day - 1));
return date;
//获取某日期的第n天后的日期
function GetDateStr(date,n)
var dd = date;
dd.setDate(dd.getDate()+n);
var y = dd.getFullYear();
var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);
var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();
return y+"-"+m+"-"+d;
;
以上是关于C#中用当前日期算周的若干问题?的主要内容,如果未能解决你的问题,请参考以下文章