To change date format with last day of the month

Hello everyone,

I would need to change this date format “yyyy/MM” (example: 2021/03) to “MM/dd/yyyy” (03/31/2021), so that there would be always last date of the month.

Could somebody help me / give me a tip how to do it?

Thank you
Marie

Hi @MarieV and welcome to our forum!

Here is a solution for you.

Step 1: For your workflow import System.Globalization (next to your Variables and Arguments you can find Imports - type it in the field and hit enter)
image

Step 2: Let’s say your date comes as string:
image

Step 3: We need to first make it a DateTime variable. We know the format so we use Date.ParseExact function

Date.ParseExact(inputDateStr,"yyyy/MM",CultureInfo.InvariantCulture)

image

Step 4: Create new DateTime variable from the previous one with a little trick:
This will take year and month from the supplied date and set first day in month and then use AddMonth(1) to increment the month and AddDays(-1) to go one day back, since that is the last day of the month

new DateTime(inputDateDateTime.Year,inputDateDateTime.Month,1).AddMonths(1).AddDays(-1)

image

Step 5: Write it out or do whatever you need with that :slight_smile:
image

You are welcome! :slight_smile:
Here is the workflow:
marie.xaml (7.4 KB)

1 Like

Great, thank you! It works for me.

1 Like