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]=
InputForm shows that internally a date is a DateObject[ ...]. You can always enter dates directly in this form.
Find the internal representation of a date:
In[4]:=
Click for copyable input
Out[4]=
Subtract two dates:
In[5]:=
Click for copyable input
Out[5]=
In[6]:=
Click for copyable input
Out[6]=
DayRange is the analog of Range for dates:
In[7]:=
Click for copyable input
Out[7]=
DayName finds the day of the week for a particular date.
Compute the day of the week 45 days from now:
In[8]:=
Click for copyable input
Out[8]=
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[9]:=
Click for copyable input
Out[9]=
Compute the phase of the moon on a certain date:
In[10]:=
Click for copyable input
Out[10]=
Generate an icon for the phase of the moon:
In[11]:=
Click for copyable input
Out[11]=
If you know both the date and a location on Earth, you can work out when the sun will rise and set.
In[12]:=
Click for copyable input
Out[12]=
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]=
Vocabulary
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.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[]=
Q&A
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?
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