日期为2009年6月1日,用什么代码能够获得登录日期上个月的日期?
以下是引用mr725在2009-6-19 18:00:00的发言:
是2009年5月1日 ?
对,就是通过代码求得这个日期。
2009年6月1日是变量。
关键是还要判断现在时间,要是现在时间是一月就比较麻烦了。
试一试吧~ 觉得有点怪怪的,呵呵~
dim dt as date = Date.Today
dim lsdt as date '重复了,删除调后,下面的lsdt都用dt即可了
dim m as string = dt
if dt.month = 12
lsdt = m.replace("" & m.split("-")(1) & "","1")
end if
if dt.month = 1
lsdt = m.replace("" & m.split("-")(1) & "-","12-")
end if
if dt.month >=2 and dt.month <=11
lsdt = m.replace("" & m.split("-")(1) & "","" & m.split("-")(1)-1 & "")
end if
[此贴子已经被作者于2009-6-19 19:19:02编辑过]
这个更规范:
dim y as string = Date.Today.year
dim m as string = Date.Today.month
if m = 1
Dim Days As Integer = Date.DaysInMonth(y,13-1)
dim dt as date = Date.Today.adddays(-days)
else
Dim Days As Integer = Date.DaysInMonth(y,m-1)
dim dt as date = Date.Today.adddays(-days)
end if
[此贴子已经被作者于2009-6-19 21:07:55编辑过]
以下是引用mr725在2009-6-19 21:00:00的发言:这个更规范:
dim y as string = Date.Today.year
dim m as string = Date.Today.month
if m = 1
Dim Days As Integer = Date.DaysInMonth(y,13-1)
dim dt as date = Date.Today.adddays(-days)
else
Dim Days As Integer = Date.DaysInMonth(y,m-1)
dim dt as date = Date.Today.adddays(-days)
end if
[此贴子已经被作者于2009-6-19 21:07:55编辑过]
这个不错
可惜if 后少写一个then,呵呵