Explore the latest version of An Elementary Introduction to the Wolfram Language »
19Dates and Times
In the Wolfram Language, Now gives your current date and time.
In[1]:=
Click for copyable input
Out[1]=
Add a week to the current date and time:
In[2]:=
Click for copyable input
Out[2]=
Enter a date:
In[3]:=
Click for copyable input
Out[3]=
Subtract two dates:
In[4]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]=
DayRange is the analog of Range for dates:
In[6]:=
Click for copyable input
Out[6]=
DayName finds the day of the week for a particular date.
Compute the day of the week 45 days from now:
In[7]:=
Click for copyable input
Out[7]=
Once you know a date, there are lots of things you can compute. For example, MoonPhase gives the phase of the moon (or, more accurately, the fraction of the Moon that is illuminated when seen from the Earth).
Compute the phase of the moon now:
In[8]:=
Click for copyable input
Out[8]=
Compute the phase of the moon on a certain date:
In[9]:=
Click for copyable input
Out[9]=
Generate an icon for the phase of the moon:
In[10]:=
Click for copyable input
Out[10]=
If you know both the date and a location on Earth, you can work out when the sun will rise and set.
In[11]:=
Click for copyable input
Out[11]=
In[12]:=
Click for copyable input
Out[12]=
Theyre about a minute off from being exactly 1 day (24 hours) apart:
In[13]:=
Click for copyable input
Out[13]=
Time zones are one of many subtleties. LocalTime gives the time in the time zone of a particular location.
In[14]:=
Click for copyable input
Out[14]=
In[15]:=
Click for copyable input
Out[15]=
Among the many areas where the Wolfram Language has extensive data is weather. The function AirTemperatureData uses this data to give the historical air temperature at a particular time and place.
Find the air temperature here at 6 pm yesterday:
In[16]:=
Click for copyable input
Out[16]=
If you provide a pair of dates, AirTemperatureData computes a time series of estimated temperatures between those dates.
Give a time series of air temperature measurements from a week ago until now:
In[17]:=
Click for copyable input
Out[17]=
DateListPlot is the analog of ListPlot for time series, where each value occurs at a particular date.
Plot the list of air temperature measurements:
In[18]:=
Click for copyable input
Out[18]=
As another example, lets look at data that goes much further back in time. WordFrequencyData tells one how frequently a particular word occurs, say in a sample of books published in a given year. Theres a lot of history one can see by looking at how this changes over the years and centuries.
In[19]:=
Click for copyable input
Out[19]=
Cars started to exist around 1900, but gradually stopped being called automobiles:
In[20]:=
Click for copyable input
Out[20]=
WordFrequencyData is set up to make it easy to compare frequencies of different words. Lets see how monarchy and democracy have fared over the years. Democracy is definitely more popular now, but monarchy was more popular in the 1700s and 1800s.
Compare historical word frequency between monarchy and democracy:
In[21]:=
Click for copyable input
Out[21]=
19.1Compute how many days have elapsed since January 1, 1900. »
Sample expected output:
Out[]=
19.2Compute what day of the week January 1, 2000 was. »
Sample expected output:
Out[]=
19.3Find the date a hundred thousand days ago. »
Sample expected output:
Out[]=
19.4Find the local time in Delhi. »
Sample expected output:
Out[]=
19.5Find the length of daylight today by subtracting todays sunrise from todays sunset. »
Sample expected output:
Out[]=
19.6Generate an icon for the current phase of the moon. »
Sample expected output:
Out[]=
19.7Make a list of the numerical phase of the moon for each of the next 10 days. »
Sample expected output:
Out[]=
19.8Generate a list of icons for the moon phases from today until 10 days from now. »
Sample expected output:
Out[]=
19.9Compute the time today between sunrise in New York City and in London. »
Sample expected output:
Out[]=
19.10Find the air temperature at the Eiffel Tower at noon yesterday. »
Sample expected output:
Out[]=
19.11Plot the temperature at the Eiffel Tower over the past week. »
Sample expected output:
Out[]=
19.12Find the difference in air temperatures between Los Angeles and New York City now. »
Sample expected output:
Out[]=
19.13Plot the historical frequency of the word groovy»
Expected output:
Out[]=
+19.1Compute how many weeks have elapsed since January 1, 1900. »
Sample expected output:
Out[]=
+19.2Compute the time between 3 pm today and sunset today. »
Sample expected output:
Out[]=
+19.3Generate an icon of the phase of the moon on August 29, 1959. »
Sample expected output:
Out[]=
+19.4Make a line plot of the numerical phase of the moon for each of the next 30 days. »
Sample expected output:
Out[]=
+19.5Make a Manipulate of the icon for the moon phase over the next 15 days. »
Sample expected output:
Out[]=
+19.6Show in a column the times of sunrise for 10 days, starting today. »
Sample expected output:
Out[]=
+19.7Plot together the historical frequencies of the words science and technology»
Expected output:
Out[]=
How can I get a date as a string?
Use DateString[date]. There are many options for the format of the string. For example, DateString[date, "DateShort"] uses short day and month names.
How can I extract the month or some other element from a date?
Use DateValue. DateValue[date, "Month"] gives the month number, DateValue[date, "MonthName"] gives the month name, etc.
How far in the past can dates be in the Wolfram Language?
Why are sunrise and sunset given only to the minute?
Because you cant compute more accurately than that exactly when the sun will actually rise and set without knowing things like air temperature that affect the bending of light in the Earths atmosphere.
Where does the Wolfram Language get air temperature data from?
What is a time series?
Its a way of specifying the values of something at a series of times. You can enter a time series in the Wolfram Language as TimeSeries[{{time1, value1}, {time2, value2}, ...}]. The Wolfram Language lets you do arithmetic and many other operations with time series.
What does DateListPlot do?
It plots values against times or dates. The values can be given in a TimeSeries[...] or in a list of the form {{time1, value1}, {time2, value2}, ...}.
 
Download Notebook Version
es