Difference between revisions of "Date Functions"

Line 1: Line 1:
[[Category:Date Functions]]
[[Category:Date and Time Functions]]
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->

Revision as of 22:15, 22 January 2008

What's new in Analytica 4.0? >

Analytica 4.0 provides much greater flexibility for formatting dates -- in US and ROW (rest of the world) formats. It also adds four useful functions for computing with dates.

Date formats

You can control whether and how a number is displayed as a date in the Number Format dialog, available from the Result menu:

Number format.png

The Date format in the Number Format dialog offers these options:

Short -- e.g., 8/5/2006
Abbrev -- e.g., Aug-5-2006
Long -- e.g., Thursday, 05 August, 2006

The actual formats for the above dates depend on settings in the Regional and Language Options from the Windows Control Panel. See below for details.

Custom -- lets you select an existing custom format or make a new one. These are some examples
dd-MM-yy → 05-8-06
'Q'Q YYYY → Q2 2006
www, d MMM yyyy → Thu, 5 Aug 2006
wwww, d of MMMM, yyyy → Thursday, 5 of August, 2006

The Custom date format uses these letter codes (standard conventions from Microsoft Windows):

d: numerical day of the month -- 1, 2,... 31
dd: numeric day of the month with two digits -- 01, 02,...31
ddd: abbreviated ordinal day of month - 1st, 2nd, ..., 31st
dddd: ordinal day of month -- first, second, ..., thirty-first
Dddd: capitalized ordinal day of month -- First, Second, ... Thirty-first
www: weekday in three letters -- Mon, Tue,.. Sun
wwww: weekday in full -- as Monday, Tuesday, ... Sunday
M: month as a number -- 1, 2, ... 12
MM: month as two-digit number -- 01, 02, ...12
MMM: month as three letter name -- Jan, Feb, ... Dec
MMMM: month as full name -- January, February, ... December
q: quarter as one digit -- 1, 2, 3, 4
yy: year as two digits -- e.g., 99, 00, 01
yyyy: year as four digits -- e.g., 1999, 2000, 2001
Any other characters, including space, appear literally as given.
To show a character literally that is also a code, enclose it in quotes, e.g. 'q'q → q2

If you specify any 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, a variable with a date format, interprets "9/11/2001" as "11 September, 2001" on a computer set to USA region or "9 November, 2001" elsewhere. With no date format, it interprets "9/11/2001"as ((9 divided by 11) divided by 2001) = 0.000409!

Regional and Language settings

The names of days and months and the formats used for Short, Abbrev and Long dates depend on the regional settings for Windows. So, a user in the US may see a short date as 9/11/2001, while a user in Denmark may see 11/9/2001. You can review the settings in Regional and Language Options from the Windows Control Panel. To modify them, cick Customize button and select teh Date tab or Languages tab. For example, if you set the language to Spanish (Argentina), Makedate(2007, 2, 3) with the Long date setting displays as:

Sábado, 03 de Febrero de 2007.

Date values and the date origin

Analytica represents a date as a date value -- that is, the number of days since the date origin. By default the date origin is Jan 1, 1904 -- as used by most Macintosh applications, including Excel on Macintosh, and all releases of Analytica on Macintosh and Windows up to Analytica 3.1. If you check Use Excel date origin in the Preferences... dialog, the date origin is Jan 1, 1900 -- as used in Excel on Windows (unless reset) and most other Windows software.

With 'Use Excel date origin checked, the numeric value of dates are the same in Analytica and Excel for Windows for dates falling on or after 1 Mar 1900. Because of a bug in Excel, in which Excel incorrectly treats Feb 29, 1900 is a valid day (1900 was not really a leap year), dates falling before that date will not have the same numeric index in Analytica as they do in Excel.

When using models containing dates or date functions from Analytica releases 3.1 or earlier, you should keep 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 check this option.

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 date with that year, month, and day. If omitted, month and day default to 1.

MakeDate(2007,5,15) → 15-May-2007
MakeDate(2000) → 1-Jan-2000
year, month, and date should be positive integers, or coercible to positive numbers.
MakeDate(year: Coerce Positive; month, day: Optional Coerce Positive)
Special functions

DatePart(date, part)

Gives the year, month, or day as a number, given a date value date. When part='Y', it gives the four digit year as a number, 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. When part='w', it returns a number from between 1 (Sunday) and 7 (Saturday).

DatePart(MakeDate(2006, 2, 28), 'D') -> 28
date as a date number, and part as 'Y', 'M', or 'D'(upper- or lowercase).
DatePart(date: Numeric; part: Text)
Special functions
Less Common Options
part may also be: 'MMM' (returns e.g., 'Jan'), 'MMMM' (e.g., 'January'), 'ddd' (e.g., '1st'), 'dddd' (e.g., 'first'), 'Dddd' (e.g., 'First'), 'w' (e.g., 1 for Sunday, 2 for Monday, ..., 7 for Saturday), 'www' (e.g., 'Mon'), 'wwww' (e.g,. 'Monday'), 'q' (1 to 4, e.g., 1 for Jan-Mar).
More Examples and Tips

This makes a sequence of weekdays landing between date1 and date2:

Index J:=date1..date2 do Subset( DatePart(J,"w")>=2 and DatePart(J,"w")<=6 )

DateAdd(date, offset, unit)

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


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

DateAdd(MakeDate(2006,1,1), 0..12, "M") 
-> ["1 Jan 2006", "1 Feb 2006", "1 Mar 2006", ... "1 Jan 2007"]

(note: the actual values in the list returned above are numbers formatted as dates)

If an offset would appear to go past the end of a month, such as

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

it returns the last day of the month. 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. DateAdd(date,n,"WD") adds the specified number of weekdays to the first weekday equal to or falling after date.

Use a negative offset to subtract units from a date.

date and offset to be numbers, and unit to be 'Y', 'M', 'D', or 'WD' (upper or lowercase).
Syntax: DateAdd(date, offset
Number; unit: Text)
Special functions
More examples and tips
MakeDate( 2007, 2, 10)                   --> Saturday, 2007-Feb-10
DateAdd( MakeDate(2007,2,10), 0,  "WD" ) --> Monday, 2007-Feb-12
DateAdd( MakeDate(2007,2,10), -1, "WD" ) --> Friday, 2007-Feb-9
DateAdd( MakeDate(2007,2,10),365, "D" )  --> Sunday, 2008-Feb-10
MakeDate(2007,2,10) + 365                --> Sunday, 2008-Feb-10
DateAdd( MakeDate(2007,2,10),365, "WD" ) --> Monday, 2008-July-7

The following generates a series of dates landing on the 1st and 15st of each month:

var J := 0..24;
DateAdd( MakeDate(2006,1, if Mod(J,2)=0 Then 1 Else 15), floor(J/2), "M")
[ 2006-Jan-1, 2006-Jan-15, 2006-Feb-1, 2006-Feb-15, 2006-Mar-1, ..., 2007-Jan-1 ]


Returns the date number for the day on which the function is evaluated. Unlike other functions, it gives a different value every day the model is run.

Special functions

You are not allowed to post comments.