Get Previous Months' Last Date

HI, i’m using this to obtain the last date of previous months, however the year is showing current year instead 2022; i.e. 2023-11-30

New DateTime(now.year,now.AddMonths(Period_Offset-1).Month,1).AddDays(-1).toString(“yyyy-MM-dd”)

@jerry.kohzl

Please try this

New DateTime(Now.Year,Now.Month,1).ADDDAYS(-1).ToString("yyyy-MM-dd")

cheers

cheers

HI @jerry.kohzl

How about this expression

New DateTime(Now.Year,Now.Month,1).AddDays(-1).ToString("dd/MM/yyyy")

image

Regards
Gokul

Hi Gokul, what parameter do I use set if I want 30-11-2022?

HI @jerry.kohzl

Try with this expression

New DateTime(Now.AddYears(-1).Year,Now.AddMonths(-DaysInt).Month,1).ToString("dd/MM/yyyy")

image

Regards
Gokul

It returned 30-12-2022

Updated the post? @jerry.kohzl

Hello @jerry.kohzl
Try this

DateSerial(now.Year,now.Month,0).ToString("dd-MM-yyyy")   ==> 31-12-2022

DateSerial(now.Addmonths(-1).Year,now.Addmonths(-1).Month,0).ToString("dd-MM-yyyy")    ==> 30-11-2022

image

image

here the year is fixed, and the add month will not decrease the year when the date is constructed

have a look here:
grafik

now.addDays( - now.Day + 1).addMonths(- intYourOffset + 1).AddDays(-1).toString("yyyy-MM-dd")

Thanks! This works!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.