19 | Dates and Times |

In[1]:= |

Out[1]= |

Add a week to the current date and time:

In[2]:= |

Out[2]= |

Enter a date:

In[3]:= |

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]:= |

Out[4]= |

Subtract two dates:

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

Compute the day of the week 45 days from now:

In[8]:= |

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]:= |

Out[9]= |

Compute the phase of the moon on a certain date:

In[10]:= |

Out[10]= |

Generate an icon for the phase of the moon:

In[11]:= |

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]:= |

Out[12]= |

In[13]:= |

Out[13]= |

Time zones are one of many subtleties. LocalTime gives the time in the time zone of a particular location.

In[14]:= |

Out[14]= |

In[15]:= |

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]:= |

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]:= |

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]:= |

Out[18]= |

Vocabulary

Now | current date and time | |

Today | date object for today | |

Tomorrow | date object for tomorrow | |

Yesterday | date object for yesterday | |

DayRange[date_{1},date_{2}] | list of dates from date_{1} to date_{2} | |

DayName[date] | day of the week of date | |

MoonPhase[date] | moon phase on date | |

Sunrise[date,location] | time of sunrise on date at location | |

Sunset[date,location] | time of sunset on date at location | |

LocalTime[location] | current time at location | |

AirTemperatureData[location,time] | air temperature at time at location | |

AirTemperatureData[location,{time_{1},time_{2}}] | time series of air temperatures from time _{1} to time_{2} at location | |

DateListPlot[timeseries] | plot a time series |

19.1Compute how many days have elapsed since January 1, 1900. »

19.2Compute what day of the week January 1, 2000 was. »

19.3Find the date a hundred thousand days ago. »

19.4Find the local time in Delhi. »

19.6Generate an icon for the current phase of the moon. »

19.7Make a list of the numerical phase of the moon for each of the next 10 days. »

19.8Generate a list of icons for the moon phases from today until 10 days from now. »

19.9Compute the time today between sunrise in New York City and in London. »

19.10Find the air temperature at the Eiffel Tower at noon yesterday. »

19.11Plot the temperature at the Eiffel Tower over the past week. »

19.12Find the difference in air temperatures between Los Angeles and New York City now. »

+19.1Compute how many weeks have elapsed since January 1, 1900. »

+19.2Compute the time between 3 pm today and sunset today. »

+19.3Generate an icon of the phase of the moon on August 29, 1959. »

+19.4Make a line plot of the numerical phase of the moon for each of the next 30 days. »

+19.6Show in a column the times of sunrise for 10 days, starting today. »

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?

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[{{time

_{1}, value_{1}}, {time_{2}, value_{2}}, ...}]. 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 {{time

_{1}, value_{1}}, {time_{2}, value_{2}}, ...}.- The Wolfram Language decides whether to interpret a date like 8/10/15 as month/day/year or day/month/year based on what country you’re in. You can pick the other interpretation if you want.
- Monday, etc. are
*symbols*with intrinsic meaning, not strings. - There are many subtleties in handling dates. An example is deciding when a particular day starts, accounting for time zones, etc. DateObject has options to address such issues.

More to Explore