How to get current month minus 18 months i.e for example this is November2023 and I want to get May 2022 as output. and input should be first day of that month
Hi @Sneha_1992
currentDate: DateTime.Now
date18MonthsAgo : currentDate.AddMonths(-18)
firstDayOfPreviousMonth: new DateTime(date18MonthsAgo.Year, date18MonthsAgo.Month, 1)
Message Box: "First day of the month 18 months ago: " + firstDayOfPreviousMonth.ToString("MMMM, yyyyy")
currentDate, date18MonthsAgo, firstDayOfPreviousMonth is of datatype System.DateTime
Hope it helps!!
StartDate of type DateTime
StartDate =DateSerial(Now.AddMonths(-18).Year, Now.AddMonths(-18).Month, 1)
Hope this will help you
now.AddMonths(-18).ToString("MMM yyyy")
gives you the month with year
new datetime(now.AddMonths(-18).Year,now.AddMonths(-18).Month,1).ToString("dd/MM/yyyy")
gives you the date
1 Like
YourDateTimeVar.AddDays(- (NowYourDateTimeVarDay - 1)).AddMonths(-18)
ADDED: When Time part needs to be 00:00:00 handling:
Thanks @Shiva_Nikhil
Same llike this how to get last day of the month of 18 months ago month.
1 Like
new datetime(now.AddMonths(-18).Year,now.AddMonths(-18).Month,1).AddDays(-1).ToString("dd/MM/yyyy")
use this to get last day of the month which is 18 months ago