Changing Date output formatting

Hi guys quick question i have the following output for a date
Feb 21 2020

I need to rearrange to year / month / date instead, how do i do this?

Hi @Adeel_Ahmed

Try this one

CDate(row(“Your Date”).Tostring).Tostring(“yyyy/MM/dd”)

Regards,
Kommi Jeevan

2 Likes

Datetime.ParseExact(“Feb 21 2020”,“MMM dd yyyy”,system.Globalization.CultureInfo.InvariantCulture).ToString(“yyyy/MM/dd”)

where you can change “Feb 21 2020” to a general variable (e.g. MyDate) and where you can find the logic behind “MMM dd yyyy” here, Custom date and time format strings | Microsoft Docs

1 Like

@kommijeevan @AndersJensen Thanks for the reply

The date is stored in a Variable called CompDate2 i can’t insert that variable into either of the code you provided

@Adeel_Ahmed- you can try

Datetime.parseexact(“Feb 21 2020”,“MMM dd yyyy”,system.globalization.cultureinfo.invariantculture).to string(“yyyyMM/dd”)

1 Like

Datetime.ParseExact(CompDate2,“MMM dd yyyy”,system.Globalization.CultureInfo.InvariantCulture).ToString(“yyyy/MM/dd”)

1 Like

Ok this kind of worked however it get me the Month as a value of 02, i need it to stay as feb etc

Hi @Adeel_Ahmed

Try this YourDate.ToString(“yyyy/MM/dd”)

Regards,
Kommi Jeevan.

1 Like

Adding to AndersJensen - If you want the month in the text form you can use “MMM” instead of “MM”. Updated code will look like:

Datetime.ParseExact(CompDate2,“MMM dd yyyy”,system.Globalization.CultureInfo.InvariantCulture).ToString(“yyyy/MMM/dd”)

3 Likes

This looks like it worked great thank you! I removed the / in the brackets after the .tostring since it matched the original formatting i needed! Thanks

Then you just change it to

Datetime.ParseExact(CompDate2,“MMM dd yyyy”,system.Globalization.CultureInfo.InvariantCulture).ToString(“yyyy/MMM/dd”)

6 Likes

Ok so the second part of this i’ve got to parse another date the result i am getting right now is:

Submitted on: Feb 27 2020 11:19 AM (EST)

I’ve successfully removed submitted on: - by using AppEvent = AppEvent.Remove(0,14)

Which gives the result: Feb 27 2020 11:19 AM (EST)

However i can’t figure out the code to remove everything after 2020 (11:19 AM (EST)) As the only result i want is Feb 27 2020, AppEvent is the variable name.

Hey Anders any thought to the below?

Put in this:
System.Text.RegularExpressions.Regex.Match(AppEvent,"(?<=Submitted on: )(\w{3} \d{2} \d{4})".Value

Let me know, if that solved it :blush:

Kind regards, Anders

2 Likes

You can simply use this activity and change the date format as you want Date Format Converter - RPA Component | UiPath Marketplace

Anders has provided a regex method to solving your problem that is essentially an excellent go-to-solution for complex parsing conditions ( you should strive to learn it as it can do wonders).

In this particular case, his regex, in plain language, is essentially saying “i will extract anything portion that have the following specific formatting conditions (\w{3} \d{2} \d{4}) that must occurs right after “the submitted on” word group” (?<=Submitted on: ), while not caring whether there are any timezone or whatever string after it."

For a start though, you can try combining the more basic string methods: Indexof, remove, substring, replace, length, etc. Note: Regex also has these functions, though they are slightly more specific to regex language (think of new lines as part of your searching requirements).

Basic String Parsing Examples:
Eg, Assuming that the string “submitted on:” and date format is fixed

  1. Let SampleStr = Submitted on: Feb 27 2020 11:19 AM (EST)
  2. Output: SampleStr.Remove(SampleStr.IndexOf(“2020”) + 4).Substring(13).Trim

Breakdown of output SampleStr.Remove(SampleStr.IndexOf(“2020”) + 4).Substring(13).Trim:

  1. Original >> Submitted on: Feb 27 2020 11:19 AM (EST)
  2. After Remove(SampleStr.IndexOf(“2020”) + 4) >> Submitted on: Feb 27 2020
  3. After Substring(13).Trim >> Feb 27 2020

Note: The remove function has to be executed first before the substring function since the indexof function is based on the original string, but the remove function is based on the currently edited string.
Executing the following parsing method below will give an error.

Breakdown of output SampleStr.Substring(13).Remove(SampleStr.IndexOf(“2020”)+4).Trim

  1. Original >> Submitted on: Feb 27 2020 11:19 AM (EST)
  2. After Substring(13).Trim >> Feb 27 2020 11:19 AM (EST)
  3. After Remove(SampleStr.IndexOf(“2020”) + 4) >> Feb 27 2020 11:19 AM (ES

The length of string after step 1 is 26, while index of “2020” is 21, hence the error in step 2. To make this work, you will have to find the index of the “2020” based of the string in step 1 rather than the original.

To conclude, there are many ways to parse your required data. You can start with basic string functions for simple cases. As parsing conditions grow more complex, regex is your savior.

Happy learning.

2 Likes

Wow this is awesome! Thank you, someone on the forum did a write up similar to this but didn’t explain how to bring remove and substring together but this now makes a lot more sense as when i tried to do it i put substring next to remove and it was giving me a result i couldn’t use.

Thank you! Definitely adding this my notes.

This gave me an error Compiler error(s) encountered procession expression “System.Text.RegularExpressions.Regex.Match(AppEvent,”(?<=Submitted on: )(\w{3} \d{2} \d{4})".Value".’)’ expected.

Found the problem there was just a bracket missing before the .value, worked perfect thank you Anders

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