How to change 3 different date Format String value to DateTime

Is there any good way to change all of the following string dates to DateTime type?

20210101 ←YYYYMMDD
21/01/01 ←YY/MM/DD
01/01/2021 00:00:00 ←DD/MM/YYYY 00:00:00

The above three values are all imported in the same String variable.
So I’m looking for a way to get these three different formats to come in and still be properly stored in a DateTime type variable as January 1, 2021.

【What I did】
At first I used “Convert.ToDateTime(DateStringVariable)” to convert, but with the advent of 21/01/01, I ran into a problem where I could not convert.

And next, I was converting “21/01/01” to “2021/01/01” by putting 20 in front of it, but next came “01/01/2021 00:00:00” and I am encountering the problem of not being able to convert with “2001/01/2021 00:00:00”.

Therefore, I thought of a process to convert “21/01/01” to “2021/01/01” based on whether the target string contains “2021” or not as one method, but it turned out that I could not judge by 2021 because I needed to process 2021 in 2022.

To be honest, I have not found a good way to do this.
If you know anything about this, please let me know.

Thank you very much.

@Kirigirisu_Coin

Check below for your reference

Reference

Hope this may help you

Thanks

Hi,

Hope the following helps you

Sequence1.xaml (10.1 KB)

Regards,

2 Likes

Hi @Kirigirisu_Coin

Please give a try,

strText - your string variable

Use an switch activity and in expression give,

If(strText.Contains("/"),"format1",If(strText.Contains(":"),"format2", "Default"))

Add case in switch activity

Case : format1

Add an assign activity and give,

Date.ParseExact(“yy/MM/dd”,strText,System.Globalization.CultureInfo.InvariantCulture).ToString(“MMMM dd,yyyy”)

Case : format2

Add an assign activity and give,

Date.ParseExact(“dd/MM/yyyy hh:mm:ss”,strText,System.Globalization.CultureInfo.InvariantCulture).ToString(“MMMM dd,yyyy”)

Case : Default

Add an assign activity and give,

Date.ParseExact(“yyyyMMdd”,strText,System.Globalization.CultureInfo.InvariantCulture).ToString(“MMMM dd,yyyy”)

Thanks

1 Like

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