Private Sub Form_Load() b = DateAdd("M", 1, Date) c = Year(b) & "-" & Month(b) & "-01" d = DateAdd("d", -1, c) Debug.Print d End Sub
写个函数: '参数 1-年 2-月 Public Function LastDayOfMonth(byval Year As Long, byval Month As Long) LastDayOfMonth = DateSerial(CInt(Year), CInt(Month + 1), 0) End Function
Private Sub Form_Load() Dim aDate As Date aDate = Year(Date) & "-" & Month(Date) + 1 & "-" & "1" MsgBox aDate - 1 End Sub
楼上的如果是1月31号可能就会出错呀.
Dim a As Variant
s=format(date,"short date")
's = "2006-9-23"
a = Split(s, "-")
For i = 0 To 2
Print a(i)
Next i
End Sub
返回包含指定的年、月、日的 Variant (Date)。语法DateSerial(year, month, day)DateSerial 函数语法有下列的命名参数:部分 描述
year 必要;Integer。从 100 到 9999 间的整数,或一数值表达式。
month 必要;Integer。任何数值表达式。
day 必要;Integer。任何数值表达式。
说明为了指定某个日期,如 1991 年 12 月 31 日,DateSerial 函数中的每个参数的取值范围应该是可接受的;即,日的取值范围应在 1-31 之间,而月的取值范围应在 1-12 之间。但是,当一个数值表达式表示某日之前或其后的年、月、日数时,也可以为每个使用这个数值表达式的参数指定相对日期。以下示例中使用了数值表达式代替绝对日期。这里,DateSerial 函数返回 1990 年 8 月 1 日的 十 年 (1990 - 10) 零两个月 (8 - 2) 又一天 (1 - 1) 之前的日期;换句话说,就是 1980 年 5 月 31 日。DateSerial(1990 - 10, 8 - 2, 1 - 1)year 参数的数值若介于 0 与 29 之间,则将其解释为 2000–2029年,若介于 30 和 99 之间则解释为1930–1999年。而对所有其它 year 参数,则请用四位数值表示(如 1800)。当任何一个参数的取值超出可接受的范围时,它会适时进位到下一个较大的时间单位。例如,如果指定了 35 天,则这个天数被解释成一个月加上多出来的日数,多出来的日数将由其年份与月份来决定。如果一个参数值超出 -32,768 到 32,767 的范围,就会导致错误发生。
b = DateAdd("M", 1, Date)
c = Year(b) & "-" & Month(b) & "-01"
d = DateAdd("d", -1, c)
Debug.Print d
End Sub
'参数 1-年 2-月
Public Function LastDayOfMonth(byval Year As Long, byval Month As Long)
LastDayOfMonth = DateSerial(CInt(Year), CInt(Month + 1), 0)
End Function
Dim aDate As Date
aDate = Year(Date) & "-" & Month(Date) + 1 & "-" & "1"
MsgBox aDate - 1
End Sub
CDate(Format(CDate(" & now() & "), "yyyy-mm"))