Guidance on String/DateTime Conversion and String Manipulation

I’ve spent a huge number of hours over the past few days trying to get to grips with the unimaginable complexity of DateTime conversion from strings and understanding string operators for a really simple application - but failed! The generic Dot Net Web site is unhelpfully technical, full of jargon that, to a non-programmer, is unintelligible, and not specific to UiPath. The UiPath reference guidance is woefully inadequate.

It’s clear to me that a comprehensive UiPath reference document is needed that identifies all the available string and date operators, explains (to non-programmers) how they are used in UiPath and under what circumstances. When there appears to be several different functions (or variations of one function) to achieve the same end, what method is advised and where will some of them fail?
For example, how is one expected to know that conversion of a UK-formatted date string (en-GB culture) requires importing the System.Globalization namespace!! Also required are more intelligible compiler errors (when it’s complaining about “late binding” for example, what on earth does that mean, or more pointedly, what remedial adjustment is required to the VB expression? If all these things need to be learned and applied, they need to be explained explicitly, comprehensively and succinctly. This will avoid what I’ve had to resort to, extensive trial and error. It’s not enough to rely on posting to the discussion group - all too often the replies beg more questions than they answer.

AJ

12 Likes

While I agree we need more comprehensive documentation, I can assure you date conversion is not trivial, because of the sheer amount of formats that dates can be in. As far as I know it’s a universal computing problem :slight_smile:

<>

It isn’t trivial because it seems to have been made overly complex to deal with every single eventuality and cover date and time togethre. You need not assure me of that fact as I have struggled for many hours with what ought to be, in the vast majority of cases, relatively straightforward.

Frankly, I’m astonished at the huge variety and number of ways of converting date formats that exist and the seeming total lack of any coherent, understandable documentation online to explain some of the arcane constructs needed (such as “CultureInfo.InvariantCulture” when a simple text template would suffice (“en-GB”, for example)). Just try to find information online about really useful methods, such as Date.ParseExact, or a simple list of the main templates (eg dd/MM/yy) if you are in any doubt.

The variety of date formats is really not that large, just three concepts, day, month and year, each of which can be represented in two or three ways separated using three separators most of the time. But the number of methods seems to outnumber the number of variants!

I’ve begun to resort to splitting up date strings using string operators and reconstituting them into a form amenable to the simple conversion functions. Date before/after comparison can be achieved by creating integers of the type 20170913.

The more complex and arcane the problem, the more there is a need for either:

  1. comprehensive and simplifying documentation that cuts through all the myriad of totally bewildering variants and nuances to home in on the best and recommended few specific methods, or

  2. functions written explicitly by UiPath for UiPath to cover the main requirements (anyone needing the full complexity still has them all available) - I’ve even toyed with the idea of writing some library functions that can be invoked to convert the main UK date variants into, for example, the integer form for comparison noted above. Such workarounds ought not to be needed, imho.

AJ

6 Likes

I agree in that some things are not explained well, like I still don’t get what CultureInfo is for; I just know how to validate the syntax by using the full name “System.Globalization.CultureInfo.InvariantCulture” (if I remember right).

If this helps,
DataTime is always in the same format… MM/dd/yyyy hh:mm:ss.0
or something like that.
So, whenever you convert a date into that format you are going to get it like that no matter what (from my understanding).
You can convert a date using the Convert.ToDateTime(“datestring”) or my preferred CDate(“datestring”)

Once you have the string into a DateTime format you can use any comparison like =,<,>
For example,
CDate(“09/14/17”) > CDate(“09/13/17”) should return True.
There isn’t anything wrong with using the Integer method either.

Date.ParseExact is useful in that it lets you take out the date with your own specified format.
For example,
Date.ParseExact(“14/09/17”,“dd/MM/yy”,System.Globalization.CultureInfo.InvariantCulture)
and that will return a DateTime date which you can also use in comparisons.

However, in both DateTime conversion methods, the string must be a date, so string manipulation may still be needed to remove unwanted characters using either .Split() or .Regex().Value.

Finally,
When you have your DateTime format, you can change the format to your liking using .ToString()
For example,
CDate(“09/14/17”).ToString(“MM/dd/yyyy”)
will return 09/14/2017. Also, the chosen format can include any characters like “MM|dd|yyyy” or “MM-dd-yyyy” as well as MMM for Month name. It is very similar to what you see in the Excel formats for reference.

So, maybe you knew some of this but hopefully it simplified Date conversion for someone out there… lol. I’m always learning too.

In short,

  • Extract Date as string using either .Split or .Regex
  • Convert Date to DateTime format
  • Format DateTime to desired string format using .ToString( )

Thanks.

31 Likes

In context of dates - mainly for default formats and names of months and weekdays. You just have to remember that Invariant = US for the most part, since almost all early standards were US-centric.

2 Likes

Thanks, that’s as good a simplification of the essentials of date conversion that I have seen :blush:. I’d actually just got to much the same understanding myself, but it took hours and hours and a lot of very frustrating trial and error. If I’d had just that little amount of reference information, with some background (like the necessity for the wonderfully the arcane “System.Globalization.CultureInfo.InvariantCulture”) I would have got to where I am in just a few minutes. I am grateful to be able to check my very hard won understanding is “on the right track”.

The big problem I found is that there’s voluminous information online, loads of different ways of ostensibly doing the same things, but some of the most valuable methods are inexplicably absent (try a search for ParseExact and marvel at the paucity of information one gets back). It’s hugely difficult for a non-programmer to home in on the just the bits that are needed for most applications.

2 Likes

I’d like to add that in addition to .ToString(), the DateTime format also has many other string conversion methods. I’ve for example found .ToShortDateString() (returns only the date part) and .ToShortTimeString() (returns only the time part) very useful.

I also prefer to create a CultureInfo variable and use it together with various DateTime methods. For example myCulture = new CultureInfo(“fi-FI”)

That can be used like this myDateVar.ToString(myCulture).

1 Like

I just created and account on this forum to praise you for the info related to String / DateTime conversion. Thank you all for the quality info!

1 Like

Clayton, Thank you very much. This is an excellent explanation on how to convert dates.

Hi, ClaytonM!
Thank you for your explanation, it was very clarifying. But i still can’t convert string into datetime.
My program takes an date from another program and saves as a string. When I try to convert it keeps showing this message “Assign: String was not recognized as a valid DateTime”. I’ve tried all: Convert.ToDateTime(“datestring”), CDate(“datestring”) and Date.ParseExact(“14/09/17”,“dd/MM/yy”,System.Globalization.CultureInfo.InvariantCulture). I don’t know what to do. could you give me some help?

hello,

You are facing a problem that I had before, the problem is that UiPath has a cultureInfo by default that does not comprehend that specific format, meaning that it won’t convert it, the solution is to change the culture info for one that does comprehend it, I know is kinda strange that this is the problem, considerating you wrote InvariantCulture as cultureInfo but I had the same trouble to.

To change the culture simply write an assign with this:

CultureInfo.CurrentCulture = Globalization.CultureInfo.CreateSpecificCulture(“es-ES”)

I wrote the spanish cultureInfo due is one that allows day before month, the French culture is another that allows the same, I hope this helps

3 Likes

Thank you very much, dear R_Ma!
To be honest I’ve already made it work. I took an date from a website and first converted to string putting Mont before day and then converted to DateTime. At first it was not working for some reason that i don’t know. It was telling me that my string wasn’t a valid date to be converted to DateTime althoug it was in the right format (dd/MM/yyyy - 15/03/2016 to be more specific). Later after closing and opening UIpath again it worked for some reason that I don’t know. Since i didn’t understand what happened I’m afraid that for some reason it stops to work. So if you have any idea why did this happen I would be very glad.

Thank you very much! =)

Hello Everyone,

@b4bbler @ClaytonM

Date time format change with culture, so when ever u all r passing any dates plz mention the culture.

I have defined en-Us culture

[quote=“Gabriel_melo, post:12, topic:11561, full:true”]

Please see the code.Main.xaml (11.7 KB)

Thanks

1 Like