Year, Month, and Day parameters describe an un-representable DateTime. uipath

Description

I need you to help me. Last month this function: New DateTime(Now.Year,Now.Month,20)

it was working normally, however, today, it is giving the title error. Can you help me, please?

The function is to bring every 20th of each month. What we expect: “yyyy/MM/dd”
What is returning: “MM/dd/yyyy”

Link

Date

2025-02-11

Related UiPath products

Studio

1 Like

@gmartinelli

the format depends on the system format

if you need a specific format then use the below

(New DateTime(Now.Year,Now.Month,20)).ToString("yyyy/MM/dd") this will return the string format of date as you need

cheers

Hi @gmartinelli

You can create a string variable
dateStringVariable = New DateTime(Now.Year, Now.Month, 20).ToString(“yyyy/MM/dd”)

Hope this helps!

It looks like your issue might be caused by how Now.Month is handling transitions between months, especially if it’s the last day of the month. Try this approach to ensure that the 20th always exists:

New DateTime(Now.Year, Now.Month, Math.Min(20, DateTime.DaysInMonth(Now.Year, Now.Month)))

This ensures that if the 20th exists, it’s used; otherwise, it picks the last valid day of the month. Also, for formatting, you can use:

yourDateTime.ToString(“yyyy/MM/dd”)