Date Functions

Revision as of 02:23, 27 January 2007 by Max (Talk | contribs) (Date formats)

Date formats

(Much expanded in 4.0)

You can control whether and how a number is displayed as a date in the Number Format dialog (available from the Result menu). This dialog offers three standard date formats

short 5/26/2006 medium long

and a custom format.

If you specify a date format for an input variable or Edit table, you can enter dates in any acceptable date format (no matter which date format is specified). For example, with a date format, Analytica will interpret "9/11/2001" as "11 September, 2001" (using USA localization, or "9 November, 2001" elsewhere). Without a date format, it will interpret it as ((9 divided by 11) divided by 2001)!

Date values and the date origin

A date value is represented as the number of days since the date origin -- which is Jan 1, 1900, if Use Excel date origin in the Preferences... dialog is checked, or Jan 1, 1904 if it isn't. Jan 1, 1900 is used by default in Microsoft Excel and most other software on Windows. Jan 1, 1904 is used by most Macintosh applications, including Excel on Macintosh and all releases of Analytica on Macintosh and Windows up to Analytica 3.1.

When using models using dates or date functions from Analytica releases 3.1 or earlier, you should keep this Use Excel date origin unchecked. If you want to paste or link values from Excel for Windows or other Windows software to or from Analytica, you should keep this option unchecked.

Analytica can handle dates from 1 AD to well beyond 9999 AD. Dates earlier than the date origin are represented as negative integers. Dates use the Gregorian calendar: Years divisible by 4 are leap years, except those divisible by 100 which are not leap years, except those divisible by 400 which are leap years.

You can simply add an integer n to a date to get the date n days ahead.

MakeDate(year, month, day)

Gives the date value for the day with specified year, month, and day. If omitted, month and day default to 1.


MakeDate(2006,5,27) --> 27-May-2006

Requirements: year, month, and date should be positive, or coercible to positive numbers.

MakeDate(year: Coerce Atomic Positive; month, day: Optional Coerce Atomic Positive)

DatePart(date, part)

Gives the number of the year, month, or day portion of date d, according to whether part is "Y", "M", or "D". When part="Y", it gives the year as four digits, such as 2006. When part="M", it gives a number between 1 and 12. When part="D", it gives a number between 1 and 31.

DatePart( date : atomic numeric ; part : textual atomic )

DateAdd(date, offset, unit)

Gives the date value of the day that is offset years, months, or days from date, according to whether unit is "Y", "M", or "D".

DateAdd is especially useful for generating sequences of dates for a time index. For example:

DateAdd( MakeDate(2006,1,1), 0..12, "M")

Returns the sequence ["1 Jan 2006","1 feb 2006", "1 Mar 2006", ..., "1 Jan 2007"]

If an offset results in an illegal date, such as would happen with these

DateAdd( MakeDate( 2004,2,29), 1, "Y" )    --> returns 2005-Feb-28
DateAdd( MakeDate( 2006,10,31), 1, "M" )   --> returns 2006-Nov-30

then the last day of the month is returned. In the first example, the date 2005-Feb-29 does not exist, and in the second example the date 2006-Nov-31 doesn't exist, so in each case, the last day of the month is returned.

Adding a day offset, DateAdd(date,n,"D"), is equivalent to date+n, since date is represented as an integer.

Use a negative offset to subtract units from a date.

Function DateAdd( date,offset : atomic numeric ; unit : atomic textual )

You are not allowed to post comments.