Wolfram Computation Meets Knowledge

19 Dates and Times

19Dates and Times
In the Wolfram Language, Now gives your current date and time.
Get the current date and time (as of when I wrote this!):
Now
 
You can do computations on this, for example adding a week.
Add a week to the current date and time:
Now + Quantity[1, "Weeks"]
 
Use ctrl+= to enter a date in any standard format.
Enter a date:
DateObject[{1988, 6, 23}]
 
You can do arithmetic with dates, say, subtracting them to find how far apart they are.
Subtract two dates:
Now - DateObject[{1988, 6, 23}]
 
Convert the date difference to years:
UnitConvert[Now - DateObject[{1988, 6, 23}], "Years"]
 
DayRange is the analog of Range for dates:
Give a list of the days spanning from yesterday to tomorrow:
DayRange[Yesterday, Tomorrow]
 
DayName finds the day of the week for a particular date.
Compute the day of the week 45 days from now:
DayName[Today + Quantity[45, "Days"]]
 
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:
MoonPhase[Now]
 
Compute the phase of the moon on a certain date:
MoonPhase[DateObject[{1988, 6, 23}]]
 
Generate an icon for the phase of the moon:
MoonPhase[DateObject[{1988, 6, 23}], "Icon"]
 
If you know both the date and a location on Earth, you can work out when the sun will rise and set.
Compute when sunset will be today at my current location:
Sunset[Here, Today]
 
Compute the time between successive sunrises:
Sunrise[Here, Tomorrow] - Sunrise[Here, Today]
 
Sunrise[Here, Tomorrow] - Sunrise[Here, Today] - Quantity[1, "Days"]
 
Time zones are one of many subtleties. LocalTime gives the time in the time zone of a particular location.
Find the local time now in New York City:
LocalTime[Entity["City", {"NewYork", "NewYork", "UnitedStates"}]]
 
Find the local time now in London:
LocalTime[ Entity["City", {"London", "GreaterLondon", "UnitedKingdom"}]]
 
Just as the Wolfram Language has built-in knowledge about notable geographic locations, so also it has built in knowledge about notable historical events.
Find the date of the Apollo 11 moon landing:
DateObject[ Entity["MannedSpaceMission", "Apollo11"][ EntityProperty["MannedSpaceMission", "LunarLandingDate"]]]
 
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:
AirTemperatureData[Here, DateObject[Yesterday, TimeObject[{18}]]]
 
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:
AirTemperatureData[Here, {DatePlus[Today, -Quantity[1, "Weeks"]], Now}]
 
You can use ListPlot and ListLinePlot to plot values in a time series.
Plot the list of air temperature measurements:
ListPlot[ AirTemperatureData[ Here, {DatePlus[Today, -Quantity[1, "Weeks"]], Now}]]
 
ListLinePlot[ AirTemperatureData[ Here, {DatePlus[Today, -Quantity[1, "Weeks"]], Now}]]
 
The plot shows that, not surprisingly, the temperature is higher during the day than at night.
As another example, let’s 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. There’s a lot of history one can see by looking at how this changes over the years and centuries.
Find the time series of how frequently the word “automobile” occurs:
WordFrequencyData["automobile", "TimeSeries"]
 
Cars started to exist around 1900, but gradually stopped being called “automobiles”:
ListLinePlot[WordFrequencyData["automobile", "TimeSeries"]]
 
WordFrequencyData is set up to make it easy to compare frequencies of different words. Let’s 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”:
ListLinePlot[ WordFrequencyData[{"monarchy", "democracy"}, "TimeSeries"]]
 
The Wolfram Language has lots of historical real-world data. If you ask directly for something like the population of a country, it’ll give you the latest estimate it has. But you can use Dated to have it give you historical values.
The latest estimate of the population of the USA:
Entity["Country", "UnitedStates"]["Population"]
 
An estimate of the population in the year 2000:
Entity["Country", "UnitedStates"][Dated["Population", 2000]]
 
A time series of population for all dates where it’s known:
Entity["Country", "UnitedStates"][Dated["Population", All]]
 
Plot the population over time:
ListLinePlot[ Entity["Country", "UnitedStates"][Dated["Population", All]]]
 
Now current date and time
Today date object for today
Tomorrow date object for tomorrow
Yesterday date object for yesterday
DayRange[date1,date2] list of dates from date1to date2
DayName[date] day of the week of date
MoonPhase[date] moon phase on date
Sunrise[location,date] time of sunrise on date at location
Sunset[location,date] time of sunset on date at location
LocalTime[location] current time at location
AirTemperatureData[location,time] air temperature at date at location
AirTemperatureData[location,{time1,time2}] time series of air temperatures from
time1 to time2 at location
ListPlot[timeseries], ListLinePlot[timeseries] plot time series
WordFrequencyData["word","TimeSeries"] time series of word frequencies
entity[Dated["property",year]] value of a property in a certain year
entity[Dated["property",All]] time series of values of a property
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 today’s sunrise from today’s 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 London and in New York City. »
Sample expected output:
Out[]=
Expected output:
Out[]=
Sample expected output:
Out[]=
19.12Plot the temperature at the Eiffel Tower over the past week. »
Sample expected output:
Out[]=
19.13Find the difference in air temperatures between Los Angeles and New York City now. »
Sample expected output:
Out[]=
19.14Plot the historical frequency of the word “groovy”. »
Expected output:
Out[]=
19.15Find the difference in population of the UK between 2000 and 1900. »
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?
As far as you want. The Wolfram Language knows about historical calendar systems, and the history of time zones. It also has the data to accurately compute sunrise, etc. going back at least 1000 years.
Why are sunrise and sunset given only to the minute?
Because you can’t 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 Earth’s atmosphere.
Where does the Wolfram Language get air temperature data from?
The worldwide network of weather stations, located at airports and other places. If you’ve got your own air temperature measuring device, you can connect it to the Wolfram Language through the Wolfram Data Drop (see Section 43).
What is a time series?
It’s 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.
How do ListPlot and ListLinePlot work with time series?
They plot values against times or dates. The values can be given in a TimeSeries[ ...] or in a list of the form {{time1, value1}, {time2, value2}, ...}.
What types of historical information does the Wolfram Language have?
Next Section