Date Functions

(Redirected from DatePart)


4.6  •  5.0  •  5.1  •  5.2  •  5.3  •  5.4  •  6.0  •  6.1  •  6.2  •  6.3  •  6.4  •  6.5

Analytica represents a date as the number of days since Jan 1, 1904, (or since Jan 1, 1900, if you check use Excel data origin in Preferences) . You can enter or display a date in almost any format -- using US conventions or ROW (Rest Of World) using Regional and Language settings. A time is expressed as a fraction of a day. You can set the display format for a number as a date or time in the Number format dialog. You can create a date or time with MakeDate() and MakeTime(), get the current date or time with Today(), select an element of a date with DatePart(). DateAdd() lets you add a hour, day, month, or other interval to a date. It is also useful for creating an Index of dates or times.

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
Full -- e.g., 5-Aug-2006 3:50:21 PM
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. Some examples:
dd-MM-yy -- e.g., 05-8-06
'Q'Q YYYY -- e.g., Q2 2006
www, d MMM yyyy -- e.g., Thu, 5 Aug 2006
wwww, d of MMMM, yyyy -- e.g., Thursday, 5 of August, 2006
d-MMM-yyyy hh:mm:ss tt -- e.g., 5 Aug 2006 05:45:00 PM

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

d: numeric 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 set a date format for an input variable or Edit table, you can enter dates in any acceptable date format -- no matter what date format was specified. For example, a variable with a date format interprets "9/11/2001" as "11 September, 2001" on a computer set to the 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, click 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 and time values and the date origin

Analytica represents a date as a date value -- that is, the number of days since the date origin. You can simply add an integer n to a date to get the date n days ahead. The integer portion of the date value represents the number of days since the date origin. The decimal portion, if present, represents the time as a fraction of a 24-hour day starting at midnight. Excel uses the time fraction in the same manner.

The default date origin is Jan 1, 1904 -- as used by most Macintosh applications, such Excel on Macintosh. Check Use Excel date origin in the Preferences dialog if you want to paste or link values from Microsoft Excel for Windows or other Windows software to or from Analytica. That changes the date origin to Jan 1, 1900 -- the default in Excel on Windows and most other Windows software. Then numeric value of dates are the same in Analytica and Excel for Windows for dates falling on or after 1 Mar 1900. Because Excel incorrectly treats Feb 29, 1900 as a valid day (1900 wasn't really a leap year), dates falling before that date won't have the same value in Analytica as they do in Excel.

Analytica can handle dates from 1 AD to well beyond 9999 AD. Dates before 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.

MakeDate(year, month, day, valueForInvalid)

Gives the date value for the given «year», «month», and «day». If omitted, «month» and «day» default to 1.

The optional «valueForInvalid» specifies the value to return when no date has the given «year», «month» and «day». When omitted, it returns the closest valid date in the same month when possible.

MakeDate(2024, 5, 15) → 15-May-2024
MakeDate(2000) → 1-Jan-2000
MakeDate(2015, 2, 29, valueForInvalid: null) → Null
MakeDate(2015, 2, 29) → 28-Feb-2015
«year», «month», and «date» should be positive integers, or coercible to positive numbers.

MakeTime(h, m, s)

Gives the time of day as a fraction of a day, a number between 0 and 0.99999. The allowed range on the parameters are: 0 <= «h» <= 23, 0 <= «m» <= 59 and 0 <= «s» <= 59, with the exception that h can exceed 23 when encoding a duration of more than a day, in which case the result will be greater than 1.

MakeTime(15, 30, 00) → 0.645833333 { 03:30:00 PM }

To make a time on a date, simple add them:

MakeDate(2024, 1, 1) + MakeTime(15, 30, 00) → 1-Jan-2024 03:30:00 PM

The actual format of the result depends on how you specify it in Numberformat.

DatePart(date, part)

Gives the year, month, day, or other part of a date value «date» as a number. 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, by 'M' must be upper).
DatePart(date: Numeric; part: Text)
Special functions
Less Common Options
«part» may also be:
  • 'MMM' (returns e.g., 'Jan'),
  • 'MMMM' (e.g., 'January'),
  • 'EEE' (in English, e.g., 'Jan'),
  • 'EEEE' (in English, 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).
  • 'date' (the date part, dropping the time part.)
  • 'H' (hour 0 to 23),
  • 'h' (hour 1 to 12),
  • 'm' (minute 0 to 59),
  • 's' (second 0 to 59),
  • 'HH', 'hh', 'mm', 'ss' (two digit text, e.g., "03"),
  • 'tt' ("AM" or "PM").
  • 'time' (the time part only, dropping the date part)
Elapsed days
  • 'wd' number of weekdays since the date origin.
  • 'wd' and 'wd+' counts the indicated day,
  • 'wd-' does not count the indicated day,
  • '#d' day number in current year,
  • '#w' week number in current year, week starts on Sunday,
  • '#wm' week number in current year, week starts on Monday,
  • 'e#w' and 'e#wm' (European convention, week number in current year, week 1 is the first week with at least 3 days, with 'e#w' week starts on Sunday, with 'e#wm' week starts on Monday).
More Examples and Tips

This makes a sequence of weekdays between date1 and date2:

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

This computes the number of weekdays between day1 and day2, including day1 and day2 (if day1 or day2 land are weekdays). When day2 < day1, the result is negative.

DatePart(day2, "wd+") - DatePart(day1, "wd-")

DateAdd(date, offset, unit)

Gives a date value that is «offset» from «date» by «unit» "Y" (years), "Q" (quarters) "M" (months) "D" (days), <coed>"WD" (weekdays, skipping over Saturdays and Sundays. It also works for time increments: "h" (hours), "m" (minutes) or "s" (seconds).


DateAdd() is especially useful for generating a sequence 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 display format for the dates depends on what was set in Number format.

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, it returns the last day of the month.

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', 'Q', 'M', 'D', 'WD', 'h', 'm', or 's'. The unit is not case sensitive except for 'M' (months) and 'm' (minutes).
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

This generates a series of dates with the 1st and 15th 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 (number of days since base date, usually Jan 1, 1904, as in Excel) for the day on which the function is evaluated. Unlike other functions, it gives a different value every day the model is run.

Today(withTime, utc)

Today() accepts two optional flags: Setting «WithTime» to True returns the current time of day in the fractional part of the result, by default for your local time zone. If you set «utc» to true, it returns the date and time in Universal Time Coordinated (UTC), a.k.a. Greenwich Mean Time.

Note that the result is the date and time when the function is evaluated in this session. The result is cached in the value of a variable that uses Today(). It will not be updated when the date or time changes unless you trigger a re-evaluation -- e.g. by reloading the model or by changing another input to the variable that uses Today().

Special functions


Returns True (1) if <x> is a date-time value, e.g. created by MakeDate() or MakeTime().

See also


You are not allowed to post comments.