Jumat, 05 April 2019

[MS_AccessPros] Re: Date calculations

 

Art - First and last date of month can be done with one line of code. 


'First day of month:
datFirstDate = DateSerial(Year(varDate), Month(varDate), 1)

'Last Day of month:
datLastDate = DateSerial(Year(varDate), Month(varDate) + 1, 0)


First date of a week is a bit more involved. I've shown functions for Monday and for Sunday. Thanks to my good friend AD Tejpal;

Function GetMondayDateForWeek(WkNum As Variant, YrNum As Variant) As Variant
'Purpose  : Gets date on monday falling in the given week for given year.
'           (Based upon Sunday as first day of the week)
'Note:      If the week's Monday is a date belonging to prev year, it is OK.
'DateTime : 5/1/2007 13:58
'Author   : A.D. Tejpal
    Dim DtYrStart As Date
    Dim WkDay As Long
    Dim DtFirstMonday As Date
    
    If IsNull(WkNum) Or IsNull(YrNum) Then Exit Function

    DtYrStart = DateSerial(YrNum, 1, 1)
    WkDay = Weekday(DtYrStart)
    DtFirstMonday = DtYrStart + (2 - WkDay)

    GetMondayDateForWeek = _
            DateAdd("ww", (WkNum - 1), DtFirstMonday)

End Function

Function GetSundayDateForWeek(WkNum As Variant, YrNum As Variant) As Variant
'Purpose  : Gets date on Sunday falling in the given
'           week for given year
'           (Based upon Sunday as first day of the week)
'Note:      If the week's Sunday is a date belonging to prev year, it is OK.
'DateTime : 5/1/2007 13:58
'Author   : A.D. Tejpal
    Dim DtYrStart As Date
    Dim WkDay As Long
    Dim dtFirstSunday As Date

    If IsNull(WkNum) Or IsNull(YrNum) Then Exit Function

    DtYrStart = DateSerial(YrNum, 1, 1)
    WkDay = Weekday(DtYrStart)
    dtFirstSunday = DtYrStart + (1 - WkDay)

    GetSundayDateForWeek = _
            DateAdd("ww", (WkNum - 1), dtFirstSunday)

End Function

-Bill

---In MS_Access_Professionals@yahoogroups.com, <dbalorenzini@yahoo.com> wrote :

I need help with calculating start and end dates for week, month and year. So far I have the following:


month:
    Me!txtdatefrom = CDate("01/" & Month(Date) & "/" & Year(Date))
    Me!txtDateTo = DateAdd("d", -1, DateAdd("m", 1, Me!txtdatefrom))

I know the txtdatefrom is right. It takes me to January but I need the first date of any month.

weekday

  Me!txtdatefrom = DateAdd("d", (today * -1) + 2, Date)
    Me!txtDateTo = DateAdd("d", 6 - today, Date)

Year:


    Me!txtdatefrom = CDate("01/01/" & Year(Date))
    Me!txtDateTo = DateAdd("d", -1, DateAdd("yyyy", 1, Me!txtdatefrom))

Any advice would be great!\

Thank you
Art
Sioux Falls, SD


__._,_.___

Posted by: wrmosca@comcast.net
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (2)

Have you tried the highest rated email app?
With 4.5 stars in iTunes, the Yahoo Mail app is the highest rated email app on the market. What are you waiting for? Now you can access all your inboxes (Gmail, Outlook, AOL and more) in one place. Never delete an email again with 1000GB of free cloud storage.


SPONSORED LINKS
.

__,_._,___

Tidak ada komentar:

Posting Komentar