Manipulating DateTime

Hi,
My current problem at the moment is that I want to get the current date and specify which month that date is in based on a condition. The condition is that +5 days from the start of a month, the bot will consider it the previous month. For example if the current date is between 1st of March and 5th of March inclusive, it will display the message “February 2019”.

Please help thanks.

HI @anon5199880

I have worked on a solution for you… it’s quite simple. Checkout the attached workflow

DateCheck.xaml (6.6 KB)

Let me know whether this suits your requirement… If it solves the problem, please mark the answer as the solution so that it will help others who are looking for similar solutions :slight_smile:

Use the condition Date.day < 6 in an if statement, where date is the variable with the date you have started with

Hi @Lahiru.Fernando,
Thanks a lot this almost solved all of my issues, but after going through the workflow I am concerned about if the date was 01/01/2019 for example. As the message box will display CurrentDate.Year.ToString, wouldn’t the output become something like December 2019?

1 Like

Oh… yeah… in my solution it will display as December 2019… My bad i didnt handle that in the code. :frowning:

But you can easily check that using another IF condition… same as we check the day.
In your sample date…
If date’s month is January & day is <= 5
Then CurrentDateVariable.Year.AddYear(-1)
Else
You xan display the same current year…

Its very much similar to the existing if condition

1 Like

Say if I wanted just the last two digits of the year, would this be the correct statement?
CurrentYear = CurrentDate.AddYears(-1).ToString(“yy”)

EDIT: Never mind fixed the issue

Thanks, I believe this will solve the issues now


Is it correct if I put the condition equal to “01” for January?

Yes… that looks good.

And yeah… you can try “1” as well… just try a debug and make sure whether it captures 1 or 01 for the month…

1 Like

Thanks for the advice, it wasn’t reading “01” as January so “1” is the correct one for the IF statement.

1 Like

You could simplify the whole logic by using a DateTime object as your effective date
DateTime effectiveDate = DateTime.Today.AddDays(-5)
And use that whenever you need to check .Month or .Year etc.
This has an added benefit of being able to test corner cases (you can mock it easily) like year changes etc. and its consistent.
Storing day, month and year in separate variables is just inviting an error to happen.

3 Likes

My God, you are right. This simplifies the solution a lot :joy:.
image
Message Box displays in that format for my usage.
In this case, all I need is the year and month so if I just -5 days from the current date, I will get the correct effective date for my usages as the day is not that important.

1 Like

Wow… thats very very simple… didnt know we could use yyMM at the same time… Thanks for sharing…

1 Like

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