Help in date expressions.

Hi All,,,

Might be simple question but now am well confused.

I need the expression(s) to set a Start date and End date of:
1- Current Quarter, and last year same Quarter .
2- Current year, and last year before the current year.
3- Last year,
4- This month
5- last month

Thanks
Issa S.Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Please try this

Current Quarter -> DatePart ( "q", YourDatecolumn )
last year same Quarter -> DatePart ( "q", DateAdd ( "yyyy", -1, YourDateColumn ) )
Current year -> DatePart ( "yyyy", YourDatecolumn )
last year before the current year -> DatePart ( "yyyy", DateAdd ( "yyyy", -1, YourDateColumn ) )
This month -> DatePart ( "m", YourDatecolumn )
Last year -> DatePart ( "yyyy", DateAdd ( "yyyy", -1, YourDateColumn ) )
last month -> DatePart ( "m", DateAdd ( "m", -1, YourDateColumn ) )
PatHartmanCommented:
Here is a sample database that includes useful date functions ahd forms that use them so you can see how to incorporate them in your application.n
UsefulDateFunctions171121.zip
Gustav BrockCIOCommented:
You can use these functions:

1:

Public Function DateThisQuarterFirst( _
  Optional ByVal datDateThisQuarter As Date) As Date
  
  Const cintQuarterMonthCount   As Integer = 3
  
  Dim intThisMonth              As Integer
  
  If datDateThisQuarter = 0 Then
    datDateThisQuarter = Date
  End If
  intThisMonth = (DatePart("q", datDateThisQuarter) - 1) * cintQuarterMonthCount

  DateThisQuarterFirst = DateSerial(Year(datDateThisQuarter), intThisMonth + 1, 1)

End Function


Public Function DateThisQuarterLast( _
  Optional ByVal datDateThisQuarter As Date) As Date
  
  Const cintQuarterMonthCount   As Integer = 3
  
  Dim intThisMonth              As Integer
  
  If datDateThisQuarter = 0 Then
    datDateThisQuarter = Date
  End If
  intThisMonth = DatePart("q", datDateThisQuarter) * cintQuarterMonthCount
  
  DateThisQuarterLast = DateSerial(Year(datDateThisQuarter), intThisMonth + 1, 0)

End Function

Open in new window

For the previous year, use:

=DateThisQuarterFirst(DateAdd("yyyy", -1, datDateThisQuarter))
=DateThisQuarterLast(DateAdd("yyyy", -1, datDateThisQuarter))

Open in new window

2 and 3:

Public Function DateThisYearFirst( _
  Optional ByVal datDateThisYear As Date) As Date
  
  If datDateThisYear = 0 Then
    datDateThisYear = Date
  End If

  DateThisYearFirst = DateSerial(Year(datDateThisYear), 1, 1)

End Function


Public Function DateThisYearLast( _
  Optional ByVal datDateThisYear As Date) As Date
  
  If datDateThisYear = 0 Then
    datDateThisYear = Date
  End If
  
  DateThisYearLast = DateSerial(Year(datDateThisYear), 12, 31)

End Function


Public Function DatePreviousYearFirst( _
  Optional ByVal datDateThisYear As Date) As Date
  
  If datDateThisYear = 0 Then
    datDateThisYear = Date
  End If

  DatePreviousYearFirst = DateSerial(Year(datDateThisYear) - 1, 1, 1)

End Function

Public Function DatePreviousYearLast( _
  Optional ByVal datDateThisYear As Date) As Date
  
  If datDateThisYear = 0 Then
    datDateThisYear = Date
  End If
  
  DatePreviousYearLast = DateSerial(Year(datDateThisYear) - 1, 12, 31)

End Function

Open in new window

4:

Public Function DateThisMonthFirst( _
  Optional ByVal datDateThisMonth As Date) _
  As Date

  If datDateThisMonth = 0 Then
    datDateThisMonth = Date
  End If
  
  DateThisMonthFirst = DateSerial(Year(datDateThisMonth), Month(datDateThisMonth), 1)

End Function


Public Function DateThisMonthLast( _
  Optional ByVal datDateThisMonth As Date) _
  As Date

  If datDateThisMonth = 0 Then
    datDateThisMonth = Date
  End If
  
  DateThisMonthLast = DateSerial(Year(datDateThisMonth), Month(datDateThisMonth) + 1, 0)

End Function

Open in new window

5:

Public Function DatePreviousMonthFirst(Optional ByVal datDateThisMonth As Date) As Date

  If datDateThisMonth = 0 Then
    datDateThisMonth = Date
  End If
  
  DatePreviousMonthFirst = DateSerial(Year(datDateThisMonth), Month(datDateThisMonth) - 1, 1)
  
End Function


Public Function DatePreviousMonthLast( _
  Optional ByVal datDateThisMonth As Date) As Date

  If datDateThisMonth = 0 Then
    datDateThisMonth = Date
  End If
  
  DatePreviousMonthLast = DateSerial(Year(datDateThisMonth), Month(datDateThisMonth), 0)

End Function

Open in new window

/gustav

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.