获取一个月的第一个和最后一个日期 [英] Getting the first and last dates in a month

查看:127
本文介绍了获取一个月的第一个和最后一个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个组合框,可让用户选择日期和年份.我想要做的就是将其转换为我的Access查询,该查询需要一个[开始日期]和[结束日期]参数.

I have two comboboxes that lets the user select a date and year. What I want to do is to translate this into my query in access, which requires a [start date] and an [end date] parameter.

I.E.用户在组合框中选择五月"和"2013"​​

I.E. user picks "May" and "2013" in the combo boxes

我的查询是在[开始日期]和[结束日期]之间设置的,因此我想将组合框中的本月和年选择转换为包含MM的两个字符串(开始日期和结束日期,然后将它们作为命令参数传递) /DD/YYYY,MM/DD/YYYY.取两个字符串并获得第一个有效日和最后一个有效日的最佳方法是什么.我有:

My query is setup between [start date] and [end date], so I want to translate this month and year selection from the combo boxes into two strings (startdate and enddate, then pass them as command parameters) that contain MM/DD/YYYY, MM/DD/YYYY. What is the best way to take two strings and get the first valid day and last valid day. I have:

FirstDayInMonth = DateSerial( _
 Year(Date), Month(Date), 1)

LastDayInMonth = DateSerial( _
 Year(dtmDate), Month(dtmDate) + 1, 0)

但是我需要从字符串转换为日期格式,以便仅使用月份("MAY")和年份("2013"​​)来返回所选月份的第一天/最后一天?我缺少相对简单的东西了吗?

But I need to make the switch from a string into a date format to get back the first/last day of the selected month, using only the month ("MAY") and year ("2013")? Am I missing something relatively simple?

推荐答案

那么,您是否从组合框中获得了月份和年份(为整数或长整数,还没有日期)?
然后尝试以下方法:

So, do you get the month and year (as integer or long, not a date yet) from the comboboxes?
Then try this:

Dim m As Long
Dim y As Long
Dim d_from As Date
Dim d_until As Date

m = CLng("01") ' get these two from your comboboxes
y = CLng("2013") 
d_from = DateSerial(y, m, 1)

d_until = DateAdd("m", 1, d_from)
d_until = DateAdd("d", -1, d_until)

拥有第一个日期时,您可以通过添加一个月,然后再返回一天来计算第二个(月中的最后一天).

When you have the first date, you calculate the second (last day in month) by adding one month, then going one day back.

这篇关于获取一个月的第一个和最后一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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