Tiger wrote:The date is in a 'other' numeric format : 22072009 which 22 July 2009.
I'm afraid that it doesn't make any sense to have that as a numeric format.
Most date systems use integer values to represent dates. In this way you can do arithmetic operations on those date values. For example, Excel represents today's date as 40121. If you want to get next week's date you simply add 7 to it, and away you go.
However if the date is stored the way you have it, adding 7 is going to propel you forward 7 years rather than 7 days.
You could add 10,000 to get to the next month... unless the month is December, in which case it'll be some number that becomes meaningless as a date. Also, if you go from the 9th of November to the 10th of November in your format you're effectively going from 9,112,009 to 10,112,009, which makes no particular sense.
If the source data is in that format you're probably better off bringing it in as a string rather than a numeric value. That will allow you to dissect it and recombine it in any way you want. If you want to perform mathematical operations on it (except for bumping it by a month, which I'll come to) you'd generally use DayNo to convert it into a serial date in TM1 format, which would allow you to manipulate it mathematically.
To add a calendar month, which has a variable number of days, you'd manipulate the month component of the date (and the year component if necessary).
Here are some examples which are probably far longer than you need, but since I'm not sure what you're doing with the date you can just cherry pick the bits that are useful to you. I don't know whether your data source has leading zeroes for the day; if it does and all of the values are 8 characters long, you don't need to bother doing a test for the "month offset" obviously. This is in the Data tab of a process which pulled in a list of DMMYYY or DDMMYYY values into variable V1:
Code: Select all
# Change this to some location on your own system.
SC_OUTPUT = 'D:\TM1\Temp\NewDates.txt';
# Prevents problems with the Str() conversion
# of 4 digit values.
# This would normally go in the Prolog.
DatasourceASCIIThousandSeparator='';
# Determine whether it's DDMMYYYY or DMMYYYY.
# That will determine the character on which the month
# starts.
If ( Long (V1) = 8 );
l_MonthOffset = 3;
ElseIf (Long (V1) = 7 );
l_MonthOffset = 2;
Else;
ItemReject ( 'Invalid length of date.');
EndIf;
# Get the individual components fo the date. You could get them as
# a number in a single step, but I'm storing both strings and
# numerics to demonstrate various ways of manipulating them.
# Strings
s_Day = Subst ( V1, 1, l_MonthOffset - 1);
s_Month = Subst ( V1, l_MonthOffset, 2 );
# Be aware that some early versions of TM1 can only handle 2 digit year strings
# in some functions, but this code assumes a more current version.
s_Year = Subst ( V1, l_MonthOffset + 2, 4 ) ;
# Numbers
l_Day = Numbr(s_Day );
l_Month = Numbr ( s_Month);
l_Year = Numbr (s_Year);
# As an example, let's convert the components to a serial date
l_OriginalDateSerial = DayNo( s_Year | '-' | s_Month | '-' | s_Day );
# Add a calendar month. The problem is that these
# can have varying day lengths so we'll add it to the
# month component calculated above.
l_MonthNew = l_Month+1;
If ( l_MonthNew = 13 );
l_Month = 1;
l_Year = l_Year + 1;
Else;
l_Month = l_MonthNew;
EndIf;
# Store the new month and year strings. For the month
# we'll keep a leading zero.
s_Month = If( l_Month < 10, '0', '') | Trim ( Str ( l_Month, 2, 0 ) );
s_Year = Trim ( Str ( l_Year, 4, 0 ) );
# And get the new serial date...
l_NewDateSerial = DayNo( s_Year | '-' | s_Month | '-' | s_Day );
# You can then manipulate these as needed to recreate the date in the required format;
# as either string or numeric.
# This shows us the old and new dates in string format.
AsciiOutput ( SC_OUTPUT, 'Old Date', V1, 'NewDate',
s_Day | s_Month | s_Year );
# This allows us to clearly see how many days were added.
AsciiOutput ( SC_OUTPUT, NumberToString ( l_OriginalDateSerial ) ,
NumberToString ( l_NewDateSerial ) );