Date and Time Functions

Finding the date and time.

This gives the current date and time:

DateList[]

The Wolfram Language (RefLink[DateList, paclet:ref/DateList]) function returns whatever your computer system gives as the current date and time. It assumes that any corrections for daylight saving time and so on have already been done by your computer system. In addition, it assumes that your computer system has been set for the appropriate time zone.

The variable (RefLink[$TimeZone, paclet:ref/$TimeZone]) returns the current time zone assumed by your computer system. The time zone is given as the number of hours which must be added to Greenwich Mean Time (GMT) to obtain the correct local time. Thus, for example, U.S. Eastern Standard Time (EST) corresponds to time zone -5. Note that daylight saving time corrections must be included in the time zone, so U.S. Eastern Daylight Time (EDT) corresponds to time zone -4.

This gives the current time zone assumed by your computer system:

$TimeZone

This gives the current date and time in time zone +9, the time zone for Japan:

DateList[TimeZone->9]

Time functions.

You should realize that on any computer system, there is a certain "granularity" in the times that can be measured. This granularity is given as the value of the global variable (RefLink[$TimeUnit, paclet:ref/$TimeUnit]). Typically it is either about (1/100) or (1/1000) of a second.

Pausing during a calculation.

This gives various time functions:

{AbsoluteTime[],SessionTime[],TimeUsed[]}

This pauses for 10 seconds, then reevaluates the time functions. Note that (RefLink[TimeUsed, paclet:ref/TimeUsed])[] is not affected by the pause:

Pause[10];{AbsoluteTime[],SessionTime[],TimeUsed[]}

Converting between dates and absolute times.

This sets d to be the current date:

d=DateList[]

This adds one month to the current date:

DatePlus[DateList[],{1,"Month"}]

This gives the number of seconds in the additional month:

AbsoluteTime[%]-AbsoluteTime[d]

Converting from different date formats.

You can use (RefLink[DateList, paclet:ref/DateList])["string"] to convert a date string into a date list, as long as the date format is sufficiently unambiguous.

This attempts to interpret the string as a date:

DateList["June 23, 1988 - 3:55 pm"]

For more control of the conversion, you can specify the order and type of date elements appearing in the string. The elements can be strings like "Year", "Quarter", "Month", "MonthName", "Day", "DayName", "Hour", "AMPM", "Minute", or "Second".

This extracts a date using the specified elements:

DateList[{"3/5/2001",{"Month","Day","Year"}}]

If the date element delimiters contain letters or digits, these must also be specified as part of the date elements.

This extracts a date containing a letter as a separator:

DateList[{"Jun Y1988",{"MonthName"," Y","Year"}}]

Converting to different date formats.

(RefLink[DateString, paclet:ref/DateString]) is used to give a nice string representation of a date and time. The exact output format can be specified from a long list of date elements, such as "DateTime", "DayName", "HourShort", etc.

This gives the current date and time in the default format:

DateString[]

This specifies a format for the given date:

DateString[{1988,6,23,15,55,0},{"MonthName"," ","DayShort",", ","Year"," - ","Hour12Short",":","Minute"," ","AMPMLowerCase"}]

Extracting dates from a string.

You can use (RefLink[DatePattern, paclet:ref/DatePattern])[elems] as a string pattern in string matching functions. The date elements are the same as used in (RefLink[DateList, paclet:ref/DateList]), although the default date element delimiters are restricted to the /, -, :, or . characters. Other delimiters can be given explicitly in the list of date elements.

This extracts dates of the given format from a string:

StringCases["abc 12/5/2005 def 1/15/2002 ghi",x:DatePattern[{"Month","Day","Year"}]:>DateList[x]]

This extracts dates with explicit delimiters:

StringCases["abc Mar 2002 def Aug 2005 ghi",x:DatePattern[{"MonthName"," ","Year"}]:>DateList[x]]

Plotting data with date coordinates.

(RefLink[DateListPlot, paclet:ref/DateListPlot]) can be used to plot data with date or time horizontal coordinates. Dates can be lists, strings, or absolute times as with (RefLink[DateList, paclet:ref/DateList]), (RefLink[DateString, paclet:ref/DateString]), and (RefLink[AbsoluteTime, paclet:ref/AbsoluteTime]). A date specification datespec can be given to associate dates with data given as {(Subscript[y, 1]),(Subscript[y, 2]),…}. (RefLink[DateListLogPlot, paclet:ref/DateListLogPlot]) allows you to plot the data with a logarithmic vertical scale.

This gathers some financial time series data:

fd=FinancialData["GE",{{2000},{2013}}];

This plots the financial data:

DateListPlot[fd]

This plots the same data on a logarithmic scale:

DateListLogPlot[fd]

This plots monthly data which does not contain explicit dates:

DateListPlot[{1,2,4,5,7,6,10,8,12,10,15},{{2013},Automatic,"Month"}]

Timing Wolfram Language operations.

(RefLink[Timing, paclet:ref/Timing]) allows you to measure the CPU time associated with the evaluation of a single Wolfram Language expression. (RefLink[Timing, paclet:ref/Timing]) corresponds to the increase in (RefLink[TimeUsed, paclet:ref/TimeUsed]). Note that only CPU time associated with the actual evaluation of the expression within the Wolfram Language kernel is included. The time needed to format the expression for output, and any time associated with external programs, is not included.

(RefLink[AbsoluteTiming, paclet:ref/AbsoluteTiming]) allows you to measure absolute total elapsed time. You should realize, however, that the time reported for a particular calculation by both (RefLink[AbsoluteTiming, paclet:ref/AbsoluteTiming]) and (RefLink[Timing, paclet:ref/Timing]) depends on many factors.

First, the time depends in detail on the computer system you are using. It depends not only on instruction times, but also on memory caching, as well as on the details of the optimization done in compiling the parts of the internal code of the Wolfram Language used in the calculation.

The time also depends on the precise state of your Wolfram Language session when the calculation was done. Many of the internal optimizations used by the Wolfram Language depend on details of preceding calculations. For example, the Wolfram Language often uses previous results it has obtained, and avoids unnecessarily reevaluating expressions. In addition, some Wolfram Language functions build internal tables when they are first called in a particular way, so that if they are called in that way again, they run much faster. For all of these kinds of reasons, it is often the case that a particular calculation may not take the same amount of time if you run it at different points in the same Wolfram Language session.

This gives the CPU time needed for the calculation. The semicolon causes the result of the calculation to be given as (RefLink[Null, paclet:ref/Null]):

Timing[100000!;]

Now the Wolfram Language has built internal tables for factorial functions, and the calculation takes no measurable CPU time:

Timing[100000!;]

However, some absolute time does elapse:

AbsoluteTiming[100000!;]

Note that the results you get from (RefLink[Timing, paclet:ref/Timing]) are only accurate to the timing granularity (RefLink[$TimeUnit, paclet:ref/$TimeUnit]) of your computer system. Thus, for example, a timing reported as 0 could in fact be as much as (RefLink[$TimeUnit, paclet:ref/$TimeUnit]).

Time-constrained calculation.

When you use the Wolfram Language interactively, it is quite common to try doing a calculation, but to abort the calculation if it seems to be taking too long. You can emulate this behavior inside a program by using (RefLink[TimeConstrained, paclet:ref/TimeConstrained]). (RefLink[TimeConstrained, paclet:ref/TimeConstrained]) tries to evaluate a particular expression for a specified amount of time. If it does not succeed, then it aborts the evaluation, and returns either (RefLink[$Aborted, paclet:ref/$Aborted]), or an expression you specify.

You can use (RefLink[TimeConstrained, paclet:ref/TimeConstrained]), for example, to have the Wolfram Language try a particular approach to a problem for a certain amount of time, and then to switch to another approach if the first one has not yet succeeded. You should realize however that (RefLink[TimeConstrained, paclet:ref/TimeConstrained]) may overrun the time you specify if the Wolfram Language cannot be interrupted during a particular part of a calculation. In addition, you should realize that because different computer systems run at different speeds, programs that use (RefLink[TimeConstrained, paclet:ref/TimeConstrained]) will often give different results on different systems.