以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  代码在一月份时出错?  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=1004)

--  作者:mr725
--  发布时间:2008/10/28 21:55:00
--  代码在一月份时出错?
下面的代码当前时间是一月份时会出错,请前辈帮忙修改。

Dim d As Date = Date.Today
Dim Year As Integer = d.year
Dim Month As Integer = d.month
Dim StartDate As Date = New Date(Year,Month,1)
Dim EndDate As Date = New Date(Year,Month-1,Date.DaysInMonth(Year,Month-1))
OutPut.Show(startdate)
OutPut.Show(enddate)

图片点击可在新窗口打开查看此主题相关图片如下:我的代码 在1月份时出错?.jpg
图片点击可在新窗口打开查看

--  作者:smileboy
--  发布时间:2008/10/28 22:02:00
--  
下载最新版本
--  作者:贺老六
--  发布时间:2008/10/28 22:07:00
--  
应该是:

Dim EndDate As Date = New Date(Year,Month,Date.DaysInMonth(Year,Month))



--  作者:mr725
--  发布时间:2008/10/28 22:18:00
--  
以下是引用贺老六在2008-10-28 22:07:00的发言:
应该是:

Dim EndDate As Date = New Date(Year,Month,Date.DaysInMonth(Year,Month))


哈哈~ 这个是本月的最后一天,我是想要上个月的最后一天!

还有,在命令窗口中,系统时间在2-12月之间都可以正常显示。

[此贴子已经被作者于2008-10-28 22:20:23编辑过]

--  作者:lxl
--  发布时间:2008/10/28 22:31:00
--  
Dim d As Date = Date.Today
Dim Year As Integer = d.year
Dim Month As Integer = d.month
Dim StartDate As Date = New Date(Year,Month,1)
Dim EndDate As Date = StartDate.AddDays(-1)
OutPut.Show(startdate)
OutPut.Show(enddate)

--  作者:mr725
--  发布时间:2008/10/28 22:49:00
--  
哈哈,对头,谢谢版主 lxl !

我也做出来了:
Dim d As Date = Date.Today
Dim Year As Integer = d.year
Dim Month As Integer = d.Month
Dim StartDate As Date = New Date(Year,Month,1)
Dim EndDate As Date
If Month <> 1 Then
   EndDate  = New Date(Year,Month-1,Date.DaysInMonth(Year,Month-1))
Else
   EndDate  = New Date(Year-1,12,Date.DaysInMonth(Year-1,12))
End If
OutPut.Show(startdate)
OutPut.Show(enddate)

但你的更简单!!