vba 如何在VB中获得上个月的开始和结束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15430035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:09:18  来源:igfitidea点击:

How to get start and end of previous month in VB

vbams-accessaccess-vba

提问by Katana24

Im trying to create some VB code that will get the start and end of the previous month. Im able to the current month which is just:

我正在尝试创建一些 VB 代码来获取上个月的开始和结束时间。我可以到当前月份,即:

Month(DateValue(Now))

which would return 3. From there I can take away 1 to give me 2 meaning February. This is fine but what about when I Im in January and I repeat this and it gives me zero - my code will fail. Any one know how to get the previous months start and end day then?

这将返回 3。从那里我可以拿走 1 给我 2,意思是二月。这很好,但是当我在 1 月份重复此操作时,结果为零 - 我的代码将失败。有谁知道如何获得前几个月的开始和结束日?

Thanks

谢谢

回答by Fionnuala

The first day of the previous month is always 1, to get the last day of the previous month, use 0 with DateSerial:

上个月的第一天始终为 1,要获取上个月的最后一天,请将 0 与 DateSerial 一起使用:

''Today is 20/03/2013 in dd/mm/yyyy
DateSerial(Year(Date),Month(Date),0) = 28/02/2013 
DateSerial(Year(Date),1,0) = 31/12/2012 

You can get the first day from the above like so:

您可以从上面获得第一天,如下所示:

LastDay = DateSerial(Year(Date),Month(Date),0)
FirstDay = LastDay-Day(LastDay)+1

See also: How to caculate last business day of month in VBScript

另请参阅:如何在 VBScript 中计算每月的最后一个工作日

回答by Quang le ba

I have similar formula for the First and Last Day

我有类似的第一天和最后一天的公式

The First Day of the month

每月的第一天

FirstDay = DateSerial(Year(Date),Month(Date),1)

The zero Day of the next month is the Last Day of the month

下个月的零日是该月的最后一天

LastDay = DateSerial(Year(Date),Month(Date)+ 1,0) 

回答by MrBlue

firstDay = DateSerial(Year(DateAdd("m", -1, Now)), Month(DateAdd("m", -1, Now)), 1)
lastDay = DateAdd("d", -1, DateSerial(Year(Now), Month(Now), 1))

This is another way to do it, but I think Remou's version looks cleaner.

这是另一种方法,但我认为 Remou 的版本看起来更简洁。

回答by Blisteragent

This works reliably for me in my main sub.

这在我的主子中对我来说很可靠。

Dim defDate1 As Date, defDate2 As Date

'** Set default date range to previous month
defDate1 = CDate(Month(Now) & "/1/" & Year(Now))
defDate1 = DateAdd("m", -1, defDate1)
defDate2 = DateAdd("d", -1, DateAdd("m", 1, defDate1))

回答by Olle Sj?gren

Try this to get the month in number form:

试试这个以数字形式获取月份:

Month(DateAdd("m", -3, Now))

It will give you 12for December.

它会给你12十二月。

So in your case you would use Month(DateAdd("m", -1, Now))to just subract one month.

所以在你的情况下,你会Month(DateAdd("m", -1, Now))习惯只减去一个月。

回答by Adarsh Madrecha

Just to add something to what @Fionnuala Said, The below functions can be used. These even work for leap years.

只是在@Fionnuala 所说的内容中添加一些内容,可以使用以下功能。这些甚至适用于闰年。

'If you pass #2016/20/01# you get #2016/31/01#
Public Function GetLastDate(tempDate As Date) As Date
    GetLastDate = DateSerial(Year(tempDate), Month(tempDate) + 1, 0)
End Function

'If you pass #2016/20/01# you get 31
Public Function GetLastDay(tempDate As Date) As Integer
    GetLastDay = Day(DateSerial(Year(tempDate), Month(tempDate) + 1, 0))
End Function

回答by Russell

Public Shared Function GetFOMPrev(ByVal tdate As Date) As Date
    Return tdate.AddDays(-(tdate.Day - 1))
End Function

Public Shared Function GetEOMPrev(ByVal tdate As Date) As Date
    Return tdate.AddDays(-tdate.Day)
End Function

Usage:

用法:

'Get End of Month of Previous Month - Pass today's date
EOM = GetEOMPrev(Date.Today)

'Get First of Month of Previous Month - Pass date just calculated
FOM = GetFOMPrev(EOM)

回答by Johannes denToom

Try this

尝试这个

First_Day_Of_Previous_Month = New Date(Today.Year, Today.Month, 1).AddMonths(-1)

Last_Day_Of_Previous_Month = New Date(Today.Year, Today.Month, 1).AddDays(-1)