How to get the first date of the previous month

Hi all,

I have data table with dates. I need to receive first day of previous month (always). Eg. my data is 2019-12-31, I need 2019-11-01 or 2019-11-20 - result should be 2019-10-01.

Thank you for your help.

FirstDayPreviousMnth.xaml (5.9 KB)



if the date value is like in format 2019-12-31
then once after getting the datatable variable named dt use a FOR EACH ROW loop activity and mention like this using a assign activity inside the loop
row(“your date columnname”) = Datetime.ParseExact(row(“your date columnname”).ToString.substring(0,10),“yyyy-MM-dd”,system.globalization.cultureinfo.invariantculture).AddMonths(-1).ToString(“yyyy-MM”)+“01”

Cheers @Krzysztof

Hi, I assume you have gotten your answer since then, but maybe this short video about getting dynamically fist/last days of months or years would prove to be useful for new people asking the same question. I has asked myself the same question a few months ago :slight_smile:

How to get the first and last days of the previous month in UiPath

1 Like

Hi this video is useful, if I have present date. How to get first day of date: 12/31/2019. The result should be 12/01/2019.

American Format: MM/dd/yyyy
DateTime.Today.AddMonths(-1).ToString(“MM”) + “/01/” + DateTime.Today.ToString(“yyyy”)


Why do you try convert present date?:slight_smile:
I need convert past date, as my example, 12/31/2019.

I found solution:
my new date = oldDate.ToString(“MM”) + “/01/” + oldDate.ToString(“yyyy”)

May be have problemes with the change year, can use this
MyDate = “2019-01-20”
MyFirstDate = Convert.ToDateTime(MyDate).ToString(“yyyy-MM-01”)
MyLastMonth = Convert.ToDateTime(MyFirstDate).AddMonths(-1).ToString(“yyyy-MM-dd”)

1 Like

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