How do I format date to MMM-yy?

I am trying to compare two dates in a loop and if the current date is less than or equal to the date’s month and year, add it to a list.

Here is how my syntax looks:
currDate = now.Date.toString(“MMM”)+“-”+ now.Date.toString(“yy”)
if(DateTime.ParseExact(currDate, “MM-yy”, Globalization.CultureInfo.InvariantCulture) <= DateTime.ParseExact(dtData.rows(13).item(9).toString, “MM-yy”, Globalization.CultureInfo.InvariantCultureGlobalization.CultureInfo.InvariantCulture))

dtData.rows(13).item(9).toString prints as an example 12/31/2021 00:00:00.

Not sure why you’re bothering with strings, but you have MMM as your month format in currDate, and then only MM in your ParseExact.

1 Like

What the previous comment says!

You don’t need to transform the dates to strings to compare them. In fact, it is much easier, when you just compare the dates.

So parse the value as a DateTime, then compare it to Today:

DateTime.ParseExact(dtData.rows(13).item(9).toString, “MM-yy”, Globalization.CultureInfo.InvariantCulture) >= Today

Hi @nicholas.harvey

Here is the sample flow
Sequence.xaml (7.1 KB)

Are you sure that is the correct syntax? I am getting errors after typing it

1 Like

I just copy pasted that from your original post, but true, there is an error. Correction follows

@nicholas.harvey also, if you have Date object in your code, you can simply compare the MONTH and/or YEAR properties on two dates (as needed).

compareToDate = CDate(dtData.rows(13).item(9))
If(currDate.YEAR <= compareToDate.YEAR)

I don’t think you will need to compare months - going by your problem statement - I would assume you would want to add an entry such as currDate being 08-21 if your compare date is 01-22 (since it’s still before the compare date, even though month is less than current month)?
Please disregard this comment if I understood it wrong.

1 Like

If its prints as above…then your datetime.parseexact should be like below…

DateTime.ParseExact(dtData.rows(13).item(9).toString, "MM/dd/yyyy hh:mm:ss", Globalization.CultureInfo.InvariantCulture)

Please check this…

1 Like

This wasn’t the exact solution, but if definitely helped me get the right answer. I used this information and put it in a variable then compared it to todays date. Shout out to @lukasziebold tor telling me about the Today DateTime variable.

1 Like

Yes, It was not. I was just telling you how to use Datetime.parseexact…

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