# Problems getting today's date but a year ago

I need to check if a date is before or after today’s date 1 year ago, in other words “within the past year.” Using the exact time won’t work. For example “Now” 1 year ago is 3/13/2018 3:15 PM but if the date I’m looking at is 3/13/2018 9:00 AM it still counts as “within the past year.” I have to eliminate the current time from the equation.

When I have UIPath spit out these individual DateParts the values are correctly 3, 13, and 2019.

But when I put it all together the resulting date is 1/13/2019 not 3/13/2019.

What’s going on here?

Date.ParseExact(DatePart(“m”, Now).ToString + “/” + DatePart(“d”,Now).ToString + “/” + (DatePart(“yyyy”,Now) - 1).ToString, “m/d/yyyy”, System.Globalization.CultureInfo.CurrentCulture)

1 Like

That will get you the date (without time) of 1 year ago from today.

1 Like

Except if it’s a leap year, which is why I’m trying to subtract 1 from the current year.

I’m just going to manually make a list of all the leap years for the next hundred years or so and an if/then to calculate based on 365 or 366 days. I’ll be dead before the code doesn’t work LOL

Scratch that, that’s complicated because it could be this year or last year that’s a leap year, and it could be before or after Feb 29 etc

But I can use Date.Today.ToString(“mm/dd/yyyy”) then get the current year with DatePart, subtract 1, then replace the year within the string…then convert it back to a date.

It’s almost like there should be easier ways to deal with dates

hi @postwick
try this:

5 Likes

Looks like there is! @samir

@postwick, if the variable you are storing is of type String use this: Now.AddYear(-1).ToString(“define the date format you want”)

So here’s the solution I came up with.

Assign the following to a string variable…

Date.Today.ToString(“MM/dd/yyyy”).Replace(DatePart(“yyyy”,Now).ToString,(DatePart(“yyyy”,Now)-1).ToString)

Then assign to a datetime variable…

Date.ParseExact(yearAgoString,“MM/dd/yyyy”,System.Globalization.CultureInfo.CurrentCulture)

Obviously I can combine them into one statement, just haven’t done it yet.