Convert string version of DateNum to DateTime

I'm looking for an existing function to convert a string version of a DateNum and/or TimeNum to a DateTime, without having to pull it apart and put it back together in an acceptable string format before conversion to DateTime – I’ve looked at a most of the available DateTime <--> String functions, but can’t seem to find what I'm looking for.

I’d appreciate it if someone can point me to the AB docs if something already exists.

Suppose I have this scenario:


initDateTime = DateTime() ;

initDateNumStr = DateTimeToStr(initDateTime, mode = 1 ) ; // Converts the bar's DateTime to a string in DateNum() format

And a bit later on in the code, I need to convert a string version of a date into a DateNum or DateTime format, ie turning initDateNumStr back into a DateNum or DateTime.

What I've looked at so far is:

  • DateTimeConvert: converts a DateTime to other numeric formats, but not to their string equivalents

  • DateTimeFormat: converts a DateTime to a string

  • DateTimeToStr: also converts a DateTime to a string

  • StrToDateTime: converts a string into a DateTime

It looks like StrToDateTime( ''string'' ) will do what I want, but the example suggests that the input string needs to be formatted in a particular way, eg ISO YYYY-MM-DD

What I was hoping for, was to be able to input the string version of DateNum straight into StrToDateTime() without having to pre-format it.

One of the issues of reverse-engineering a DateNum, let alone its string version, is that the year component needs special treatment, due to the limitation of 7 significant digits of FLOATS used by AB when a DateTime is converted to a DateNum.

I should'a read the docs more properly!!! :blush:

The way to do it is to convert the string to a number first, and then to a date:

newDateNum = StrToNum( SomeDateNumString ) ;
newDateTime = DateTimeConvert( 2, newDateNum) ; // converts DateNum to DateTime

Simple!

1 Like

Generally all new codes should use DateTime and ISO dates. DateNums are obsolete and kept for backward compatibility only. New formulas should not use them.

2 Likes

Agreed.

Unfortunately, some of the data I have to work with comes with them embedded, which is a pain having to convert them back and forth.

If you're planning to retire/deprecate them, then I'll have to come up with my own conversion routine.