Do anyone here Know how to get the previous month of a given date..
Example
given date: 11/25/2012
Output: 10/25/2012

thank you.. i know you can help me ..

Dani AI

Generated

Short answer: prefer date-aware functions, not hand-built strings. was on the right track recommending a date function; ’s string-assembly technique can fail on locale/formatting and at month boundaries (for example when Month - 1 becomes 0 or when the target month has fewer days).

Built-in behaviour to note: .NET’s DateTime.AddMonths (used from VB.NET) and the VBA/VB6 DateAdd/DateSerial family will not return invalid calendar dates — if the target month is shorter the result is adjusted to the last valid day of that month (e.g., March 31 -> February 28/29). See the official docs for the exact behaviour. DateTime.AddMonths docs. (learn.microsoft.com)

Safer examples (do not repeat the DateAdd snippet already in the thread):

' VB.NET (recommended if using .NET)
Dim dt As Date = New Date(2012, 11, 25)
Dim prev As Date = dt.AddMonths(-1)
Console.WriteLine(prev.ToString("MM/dd/yyyy"))  ' -> 10/25/2012

The .NET method above preserves the day when possible and uses the last day of the target month when necessary. DateTime.AddMonths docs. (learn.microsoft.com)

' VB6 / VBA (robust, avoids invalid-day surprises)
Dim dt As Date
dt = #11/25/2012#
Dim y As Integer, m As Integer, lastDay As Integer, outDay As Integer

y = Year(dt)
m = Month(dt) - 1
If m = 0 Then
  m = 12
  y = y - 1
End If

' lastDay = Day( last day of month m )
lastDay = Day(DateSerial(y, m + 1, 0))
If Day(dt) > lastDay Then
  outDay = lastDay
Else
  outDay = Day(dt)
End If

Dim prevDt As Date
prevDt = DateSerial(y, m, outDay)
MsgBox prevDt  ' -> 10/25/2012

The VB6/VBA snippet uses DateSerial to build a safe date and clamps the day to the target month’s last day; see the DateSerial reference for how out‑of‑range arguments roll and how to get the last day of a month. DateSerial docs. (learn.microsoft.com)

Short caveat: pick the behaviour that matches business rules — AddMonths gives a sensible “last valid day” result automatically in .NET, while the DateSerial method above explicitly clamps the day (useful if working with legacy VBA/VB6). For VBA/Access, the DateAdd family also avoids invalid dates; consult the DateAdd docs for platform details. DateAdd docs. (learn.microsoft.com)

Recommended Answers

All 3 Replies

well i dont know the exact function name that can solve the issue but you can use of the following to generate the same:-

dt = Month(Date) - 1 & "/" & Day(Date) & "/" & Year(Date)

hope this helps you . . .

Something like this should work:

Dim dt As Date
dt = #11/25/2012#

'DateAdd is a function specifically designed for date math operations.
dt = DateAdd("m", -1, dt)
MsgBox (dt)

Here's the MSDN page

Thank you for the Help..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.