如何在VB中获得上个月的开始和结束 [英] How to get start and end of previous month in VB

查看:153
本文介绍了如何在VB中获得上个月的开始和结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

Month(DateValue(Now))

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

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?

谢谢

推荐答案

上个月的第一天总是1,要得到上个月的最后一天,用0和DateSerial:

The first day of the previous month is always 1, to get the last day of the previous month, use 0 with 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

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

这篇关于如何在VB中获得上个月的开始和结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆