0

Format strings in VBA are slightly different than format strings in .NET.

For example, yyyy-mm-dd hh:mm:ss is a valid VBA format string that renders as 2020-07-29 14:56:23, where as the corresponding .NET format string would look like yyyy-MM-dd hh:mm:ss. The ambiguity of mm in VBA's case is resolved because the mm returns minutes only when preceded by h or hh.

I'm writing a C#/.NET service that is reading from some legacy system which stores format strings in the VBA format, and now I need to convert these format strings to .NET which is what my downstream applications expect. Is there some standard way to convert a format string from VBA to .NET?

I can write my own converter if needed, but if something already exists out there, I'd really like to avoid reinventing the wheel.

To clarify: I am not looking for a way to format a string using a VBA format string in .NET. This can easily be done by referencing Microsoft.VisualBasic.Compatibility or using P/Invoke.
I am looking for a way to convert a format string from the VBA format string format, to the .NET format string format, to pass it along to code that I don't control.

GSerg
  • 76,472
  • 17
  • 159
  • 346
Dan
  • 10,480
  • 23
  • 49
  • Added clarification. I am not asking for a way to format a string using a VBA format string in .NET. I am asking for a way to convert the format string itself! – Dan Jul 29 '20 at 09:39
  • Possible duplicate of [VB6 Format function: analog in .NET](https://stackoverflow.com/q/2116244/11683) – GSerg Jul 29 '20 at 09:39
  • @GSerg please see the clarification. The VB `Format` function renders the final string. I need to convert the format string itself, which is an entirely different question. – Dan Jul 29 '20 at 09:39
  • I have read it, which is why I removed my binding close vote, even though I'm certain it is a right one, and left a non-binding one. It is way easier to format using the old format string than to attempt to change it to the new format. In many cases, such as the one shown in the duplicate, converting the format string is plain impossible. – GSerg Jul 29 '20 at 09:41
  • @GSerg unfortunately, that is what I need! My downstream applications (which I am not in control of), expect my service to return a .NET-compatible format string. – Dan Jul 29 '20 at 09:42
  • 1
    Then you will need to accept that some data will start looking differently in cases where the format string cannot be converted. Having that done, you would need a rather fancy parser to correctly understand the format string that may contain way more than `mm-dd-yy` (quoted literals, escape sequences...). If I were to do it, and I were running on Windows, I would look into [`VarTokenizeFormatString`](https://learn.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-vartokenizeformatstring) and deciphering what it returns in `pcbActual`. – GSerg Jul 29 '20 at 09:50

0 Answers0