DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Jason has posted 22 posts at DZone. View Full User Profile

Calculate First Day Of Current Month

02.01.2008
| 37125 views |
  • submit to reddit
        VB/VBA/VB.NET one-liner to calculate the start of the current month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.

DateAdd("D", -1.0 * DatePart("D", Today) + 1, Today)
    

Comments

Snippets Manager replied on Mon, 2011/03/14 - 10:03am

To get the first day of the month from today, subtract the days from the first of the month. Note: Today is a .Net function to give you todays date, and Today.Day gives you the days since last month. Dim CalcDate As DateTime CalcDate = Today.AddDays(-1.0 * Today.Day + 1)

Snippets Manager replied on Mon, 2011/03/14 - 10:03am

'To get the last day of last month from today 'Subtract the days from the beginning of the month Dim CalcDate As DateTime CalcDate = Today.AddDays(-1.0 * Today.Day)