How to Convert month and year date in string, into DateTime?

I don’t know why this is so difficult, but I have been stuck for hours just to convert a string to a DateTime.
What I want to do is, create a workflow that receives in a string in format of “Month, year”, and return a string “Previous_Month, year” or if the “Month” is “January”, it needs to return “December, Previous_year”.

For example, if the in_argument is “March, 2019”, it needs to return as out_argument “February, 2019”.
If the in_argument is “January, 2019”, it needs to return “December. 2018”.

Since I don’t wanna have a big if statement with 12 branches, I want to convert this string to datetime so I can just “AddMonth(-1)” or “AddYear(-1)”. However, I am not sure how to convert this “month, year” into DateTime.

I first parsed the string using split, in assign activity.

Assign: ParseMonthYear = in_argument.Split(CChar(" "))

Now, I got Month and Year in ParseMonthYear(0) and ParseMonthYear(1), respectively, but now I can’t convert each piece of string into DateTime. How can you do this?

1 Like

@tomato25
You can use a vb expression like below.

DateTime.ParseExact("January, 2019", "MMMM, yyyy", System.Globalization.CultureInfo.InvariantCulture).AddMonths(-1).ToString("MMMM, yyyy")

The above expression would return December, 2018

3 Likes

Hi buddy @tomato25

If you have your string in in_argument
Then use this statement to convert the string to Datetime
Out_argument = Datetime.ParseExact(in_argument,“MMMM, yyyy”,System.Gloabalization.CultureInfo.InvariantCulture).AddMonths(-1).ToString(“MMMM, yyyy”)

Hope This will work for sure
Kindly try this and let know buddy
Cheers @tomato25

1 Like

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