VB编程?输入一个日期,计算出该日期下一个月的最后一天
1. Calculate the last day of Next monthInput: a date (including: year, month, day)
Output: last day of next month (including: year,month, day)
E.g. input: ; output: 2010/11/30
2. Test your program systemically. Write your testing cases
1.人手输入任何一个日期(包括:year,month,day)
2.输出该日期下一个月的最后一天
3.日期格式例例子:2010/10/15
求高手帮忙写一个完整的程序,谢谢各位了。 本帖最后由 bcrun 于 2011-11-30 21:58:33 编辑 本帖最后由 bcrun 于 2011-11-30 21:58:11 编辑 下下个月的第一天减一天就是
方法2错了,搞反了,应该是这样
Private Sub Command1_Click()
MsgBox Format(GetNMLD1, "yyyy年mm月dd日 HH:mm:ss")
MsgBox Format(GetNMLD2, "yyyy年mm月dd日 HH:mm:ss")
End Sub
'方法1
Private Function GetNMLD1() As Date
GetNMLD1 = DateAdd("d", -1, CDate(Format(DateAdd("m", 2, Now), "yyyy-mm-01 hh:mm:ss")))
End Function
'方法2
Private Function GetNMLD2() As Date
Dim nDate As Date, _
Y As Long, _
M As Long, _
D As Long
nDate = DateAdd("m", 1, Now)
Y = Year(nDate)
M = Month(nDate)
If M = 2 Then
D = 28 + IIf((Y - 2000) Mod 4 = 0, 1, 0)
Else
D = 30 + Int((&HAB5 And (1 * (2 ^ (M - 1)))) / (2 ^ (M - 1)))
End If
GetNMLD2 = CDate(Y & "-" & M & "-" & D & " " & Format(nDate, "HH:mm:ss"))
End Function
Private Function GetNMLD2() As Date
Dim nDate As Date, _
Y As Long, _
M As Long, _
D As Long
nDate = DateAdd("m", 1, Now)
Y = Year(nDate)
M = Month(nDate)
If M = 2 Then
D = 28 + IIf((Y - 2000) Mod 4 = 0, 1, 0)
Else
D = 30 + Int((&HAD5 And (2 ^ (M - 1))) / (2 ^ (M - 1)))
End If
GetNMLD2 = CDate(Y & "-" & M & "-" & D & " " & Format(nDate, "HH:mm:ss"))
End Function
Private Sub Command1_Click()CDate 函数不必要,可以强制转换:
Dim dt As Date
dt = InputBox("输入日期", "求下月最后一天", "2010/10/15")
Print DateAdd("d", -1, CDate(Left(CStr(DateAdd("m", 2, dt)), 8) + "1"))
End Sub
Private Sub Command1_Click()DateSerial 函数 楼上的回答都是正解!!
Dim dt As Date
dt = InputBox("输入日期", "求下月最后一天", "2010/10/15")
Print DateAdd("d", -1, Left(CStr(DateAdd("m", 2, dt)), 8) + "1")
End Sub
补充:VB , 基础类