如何根据当前系统日期求出本周,本月,本年,上周,上月,上年的第一天?
Dim intWeekDay as integer
Dim stDay as DateTime '本周第一天
Dim PstDay as DateTime '上周第一天
intWeekDay = DatePart(DateInterval.Weekday, Today) '取得今天是星期几
stDay = DateAdd(DateInterval.Day, -1 * intWeekDay, Today) '计算本周第一天的日期
PstDay = DateAdd(DateInterval.Day, -7 , stDay) '计算上周第一天的日期
我这样做,但始终取出的是上一天的,请各位大大指点。
在winform上有两个DateTimePicker控件,分别为DateTimePicker1和DateTimePicker2
还有一个是ComboBox1,在ComboBox1中有“今天,本周,本月,本年,昨天,上周,上月,上年”
我的想法是在选择本周时DateTimePicker1显示本周的第一天,DateTimePicker2显示本周的最后一天。其它以此类推
搞了半天也没搞懂怎么实现。
[解决办法]
转一下就是了
Dim dt As DateTime = DateTime.Now '当前时间
Dim dayOfWeek As Integer = Convert.ToInt32(dt.DayOfWeek.ToString("d"))
Dim startWeek As DateTime = dt.AddDays(1 - (If((dayOfWeek = 0), 7, dayOfWeek))) '本周周一
Dim endWeek As DateTime = startWeek.AddDays(6) '本周周日
Dim startMonth As DateTime = dt.AddDays(1 - dt.Day) '本月月初
Dim endMonth As DateTime = startMonth.AddMonths(1).AddDays(-1) '本月月末
'DateTime endMonth = startMonth.AddDays((dt.AddMonths(1) - dt).Days - 1); //本月月末
Dim startQuarter As DateTime = dt.AddMonths(0 - (dt.Month - 1) Mod 3).AddDays(1 - dt.Day) '本季度初
Dim endQuarter As DateTime = startQuarter.AddMonths(3).AddDays(-1) '本季度末
Dim startYear As New DateTime(dt.Year, 1, 1) '本年年初
Dim endYear As New DateTime(dt.Year, 12, 31) '本年年末