一些關于日期的實用函數
时 间:2008-08-12 17:01:42
作 者:umvsoft整理 ID:43 城市:江阴
摘 要:数據處理離不開日期處理,以前經常是每次用到又重寫一次函數,挺浪費時間的。現整理了一些關于日期的實用函數,大家參考。附件中也有代碼,供下載用。
正 文:
数據處理離不開日期處理,以前經常是每次用到又重寫一次函數,挺浪費時間的。現整理了一些關于日期的實用函數,大家參考。附件中也有代碼,供下載用。
Public Function IsLeapYear(YearNumber As Integer) As Boolean
Dim dteLeapDay As Date
' if particular year is not leap year,
' dteLeapDay will become Mar-01
dteLeapDay = DateSerial(YearNumber, 2, 29)
IsLeapYear = (Month(dteLeapDay) = 2)
End Function
' Based on vdate, the function will return the
' first day of the month
Public Function FirstDayOfMonth(vdate As Date) As Date
Dim intCurrDate As Integer ' current date
intCurrDate = DatePart("d", vdate)
' Deduct intCurrDate - 1 days
FirstDayOfMonth = DateAdd("d", -intCurrDate + 1, vdate)
End Function
' Calculate first day of the month (method two)
' This method is quite straightforward
Public Function FirstOfMonth(vdate As Date) As Date
FirstOfMonth = DateSerial(DatePart("yyyy", vdate), DatePart("m", vdate), 1)
End Function
' Based on vdate, the function will
' return the last day of the month
Public Function LastDayOfMonth(vdate As Date) As Date
' Get the first day of the next month, and then deduct 1 day
LastDayOfMonth = DateSerial(DatePart("yyyy", vdate), DatePart("m", vdate) + 1, 1) - 1
End Function
' The function will return the first day of the year based on vdate
Public Function FirstDayOfYear(vdate As Date) As Date
FirstDayOfYear = DateSerial(Year(Date), 1, 1)
End Function
' The function will return the first day of the week, based on vdate
Public Function FirstDayOfWeek(vdate As Date) As Date
Dim intWeekday As Integer
intWeekday = Weekday(vdate, vbMonday)
FirstDayOfWeek = DateAdd("d", -intWeekday + 1, vdate)
End Function
' The function will return the last day of the week, based on vdate
' Monday is the first day (assumed)
Function LastDayOfWeek(vdate As Date) As Date
Dim intWeekday As Integer
intWeekday = Weekday(vdate, vbMonday)
LastDayOfWeek = DateAdd("d", -intWeekday + 1, vdate) + 6
End Function
' The function will return number of week days between two dates
Function NumberOfWeekdays(BeginDate As Date, EndDate As Date, IncludingFirstDay As Boolean) As Integer
Dim intCounter As Integer
Dim intMovingDate As Date
intCounter = 0
If IncludingFirstDay = True Then
intMovingDate = BeginDate
Else
intMovingDate = BeginDate + 1
End If
Do While intMovingDate <= EndDate ' End date inclusive
If Weekday(intMovingDate) <> vbSunday And Weekday(intMovingDate) <> vbSaturday Then
intCounter = intCounter + 1
End If
intMovingDate = intMovingDate + 1
Loop
Access软件网官方交流QQ群 (群号:54525238)
Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- 【Access高效办公】统计当...(06.30)
- 【Access高效办公】用复选...(06.24)
- 根据变化的日期来自动编号的示例...(06.20)
- 【Access高效办公】按日期...(06.12)
- 合并列数据到一个文本框的示例;...(05.06)
- 通过命令按钮让Access列表...(04.24)
- 【Access高效办公】统计当...(03.11)
- 【Access Inputbo...(03.03)
- 按回车键后光标移动到下一条记录...(02.12)

学习心得
最新文章
- 【Access财务分析示例】按月统...(08.08)
- Access查询里使用Date()...(08.05)
- 关于Access交叉表查询生成的统...(08.02)
- ACCESS做的工作日常小工具_纸...(07.30)
- Access快速开发平台进销存教程...(07.28)
- 关于Access快速开发平台2.6...(07.23)
- 【Access交叉表查询】按百分比...(07.21)
- VBA编程-ADO-关于对象早晚期...(07.17)
- Access快速开发平台--frm...(07.15)
- 1行代码实现Access与SQL ...(07.09)