[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Date Functions



PureBytes Links

Trading Reference Links

> I'm trying to export data in ascii format and I need the date to be
> in YYYYMMDD format. TS2000 has a function to export in YYYYDDMM. 

TS2k has the EL_DateStr reserved word that should do the trick 
nicely, though you might have to cheat and reverse the MM and DD 
inputs.  Except I get an "out of memory allocating string 
storage" error if I try to use it.  Ooops.

> Thanks for the suggestion, but it that only works for data after
> the year 2000. 

You could make Mark's idea work easily enough:

if Date >= 1000000 then 
	MyDate = "20" + RightStr(NumToStr(Date,0),6)
else
	MyDate = "19" + NumToStr(Date,0);

Not sure why Mark had the leading "    " in there, since Date 
should always be >= 6 digits long back to 1910.  For dates 
between 1/1/10 (100101) and 12/31/99 (991231), it's 6 digits long 
so the RightStr isn't necessary.

> My initial effort was: NumToStr((Date+19000000),0). It works, 

No it DOESN'T, as your later note observed.  TS doesn't have 
enough numeric precision to represent an 8-digit number 
(YYYYMMDD) accurately.  DON'T use this approach.

(Though I don't see why that would have run slower than what you 
were doing before.  TS doesn't slow down just because you've 
exceeded its precision limits.  You may have something else going 
on there.)

If for some reason you don't like Mark's solution -- e.g. if you 
wanted to fool with the date format more than his code allows -- 
you could do something like this:

    NumToStr(Year(Date)+1900,0) 
     + NumToStr(Month(Date),0) 
     + NumToStr(DayOfMonth(Date),0)

...except for one minor problem:  you don't get leading zeroes if 
the Month or DayOfMonth is a single digit.

EL doesn't have any good string-formatting functions, but you can 
do it yourself.  Something like this:

  vars:  Yr(""), Mo(""), Dy(""), MyDate("");
  Yr = NumToStr(Year(Date)+1900,0);
  Mo = RightStr("0" + NumToStr(Month(Date),0), 2);
  Dy = RightStr("0" + NumToStr(DayOfMonth(Date),0), 2);
  MyDate = Yr + Mo + Dy;

Messy, but it works, and it lets you insert "/"s or whatever you 
want, and put the Mo/Dy/Yr in any order you want.  There may be a 
cleaner solution but that's what comes to mind.

Gary