In SQL Server Reporting Services; How can I calculate the first and last day of the previous month?
I know I can use the expression below to get the last day of the current month, but I'm stuck when trying to find the first and last of the previous month.
=DateSerial(Year(Now()), Month(Now()), "1").AddMonths(1).AddDays(-1)
EOMONTH returns the last day of a month from a date. Here, we use the EOMONTH function to go to the last day of the previous month. Then, we add 1 to get the first day of the current month. To perform the previous example with the EOMONTH function, we need to use the formula =EOMONTH(A2,-1)+1 in cell B2.
To check if a date is the first day of the month:Use the getDate() method to get the day of the month for the given date. If the result is equal to 1 , the date is the first day of the month.
To get the first and last day of the current month, use the getFullYear() and getMonth() methods to get the current year and month and pass them to the Date() constructor to get an object representing the two dates.
There are a few ways to do this, but I've gone with the following: last_date = datetime(year, month + 1, 1) + timedelta(days=-1) . This will calculate the first date of the following month, then subtract 1 day from it to get the last date of the current month.
Just a guess based on your code working.
--previous month last =DateSerial(Year(Now()), Month(Now()), "1").AddDays(-1) --previous month first =DateSerial(Year(Now()), Month(Now()), "1").AddMonths(-1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With