Guidance on String/DateTime Conversion and String Manipulation


#1

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


Date Formatting and Conversion - Explicit UiPath Guidance and Reference Material
Universal Date Format Converter
Date Format in Excel Reading Activity
#2

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:


#3

<>

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


#4

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.


#5

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.


#6

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.


#7

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).


#8

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!