Date and Time Functions

DateList[]give the current local date and time in the form {year,month,day,hour,minute,second}
DateList[TimeZone->z]give the current date and time in time zone z
$TimeZonegive the time zone assumed by your computer system

Finding the date and time.

This gives the current date and time:
Click for copyable input

The Wolfram Language 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 $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 . Note that daylight saving time corrections must be included in the time zone, so U.S. Eastern Daylight Time (EDT) corresponds to time zone .

This gives the current time zone assumed by your computer system:
Click for copyable input
This gives the current date and time in time zone , the time zone for Japan:
Click for copyable input
AbsoluteTime[]total number of seconds since the beginning of January 1, 1900
SessionTime[]total number of seconds elapsed since the beginning of your current Wolfram System session
TimeUsed[]total number of seconds of CPU time used in your current Wolfram System session
$TimeUnitthe minimum time interval recorded on your computer system

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 $TimeUnit. Typically it is either about or of a second.

Pause[n]pause for at least n seconds

Pausing during a calculation.

This gives various time functions:
Click for copyable input
This pauses for 10 seconds, then reevaluates the time functions. Note that TimeUsed[] is not affected by the pause:
Click for copyable input
AbsoluteTime[date]convert from date to absolute time
DateList[time]convert from absolute time to date

Converting between dates and absolute times.

This sets d to be the current date:
Click for copyable input
This adds one month to the current date:
Click for copyable input
This gives the number of seconds in the additional month:
Click for copyable input
DateList["string"]convert a date string to a date list
DateList[{"string",{"e1","e2",}}]give the date list obtained by extracting elements "ei" from "string"

Converting from different date formats.

You can use 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:
Click for copyable input

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:
Click for copyable input

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:
Click for copyable input
DateString[]give a string representing current local date and time
DateString[datespec,elems]give elements elems of date and time given by datespec

Converting to different date formats.

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:
Click for copyable input
This specifies a format for the given date:
Click for copyable input
DatePattern[elems]string pattern matching a date with the given elements

Extracting dates from a string.

You can use DatePattern[elems] as a string pattern in string matching functions. The date elements are the same as used in 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:
Click for copyable input
This extracts dates with explicit delimiters:
Click for copyable input
DateListPlot[list]generate a plot from a list of data with date coordinates
DateListPlot[list,datespec]generate a plot from a list of data with dates specified by datespec
DateListLogPlot[list]generate a linear-log plot from a list of data with date coordinates
DateListLogPlot[list,datespec]generate a linear-log plot from a list of data with dates specified by datespec

Plotting data with date coordinates.

DateListPlot can be used to plot data with date or time horizontal coordinates. Dates can be lists, strings, or absolute times as with DateList, DateString, and AbsoluteTime. A date specification datespec can be given to associate dates with data given as {y1,y2,}. DateListLogPlot allows you to plot the data with a logarithmic vertical scale.

This gathers some financial time series data:
Click for copyable input
This plots the financial data:
Click for copyable input
This plots the same data on a logarithmic scale:
Click for copyable input
This plots monthly data which does not contain explicit dates:
Click for copyable input
Timing[expr]evaluate expr, and return a list of the CPU time needed, together with the result obtained
AbsoluteTiming[expr]evaluate expr, giving the absolute time taken

Timing Wolfram Language operations.

Timing allows you to measure the CPU time associated with the evaluation of a single Wolfram Language expression. Timing corresponds to the increase in 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.

AbsoluteTiming allows you to measure absolute total elapsed time. You should realize, however, that the time reported for a particular calculation by both AbsoluteTiming and 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 Null:
Click for copyable input
Now the Wolfram Language has built internal tables for factorial functions, and the calculation takes no measurable CPU time:
Click for copyable input
However, some absolute time does elapse:
Click for copyable input

Note that the results you get from Timing are only accurate to the timing granularity $TimeUnit of your computer system. Thus, for example, a timing reported as 0 could in fact be as much as $TimeUnit.

TimeConstrained[expr,t]try to evaluate expr, aborting the calculation after t seconds
TimeConstrained[expr,t,failexpr]return failexpr if the time constraint is not met

Timeconstrained 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 TimeConstrained. 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 $Aborted, or an expression you specify.

You can use 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 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 TimeConstrained will often give different results on different systems.