Clear Filters
Clear Filters

How to store partially known datetimes

2 views (last 30 days)
What is the best way in MATLAB to deal with partially known date times?
I have a list of dates and times as part of a database. For most, I know the full date and time, but for many I only know down to the day, or month, or even just year. Currently, I have them stored in a table seperated by year, month, day, and time, but I would much prefer a single field if possible.
Is there a way to use the datetime or other datatype in MATLAB to convey that information about the date and time could be missing?
  1 Comment
Walter Roberson
Walter Roberson on 7 Oct 2020
Unfortunately, datetime objects only have specific complete datetimes, or else the Not A Time object, NaT .
You could fill out the datetime objects as far as you know, using the "start" of the date (e.g., Jan 1, 00:00:00 of the year), and also keep a numeric encoding of which information is missing (perhaps an enumeration)

Sign in to comment.

Accepted Answer

Stephen23 on 8 Oct 2020
Edited: Stephen23 on 19 Oct 2020
I don't believe that information can be encoded inside one datetime object, so you will have to use another object or variable to hold that information (which could be another datetime).
Here are a few idea (all examples are known down to the nearest minute):
  1. use a calendarduration or duration object to store the lowest known unit, e.g. duration 00:01:00.000
  2. similarly with a date vector, e.g. [0,0,0,0,1,0] (downside: not scalar, but perhaps slightly easier to work with)
  3. similarly with a logical vector, e.g. [false,false,false,false,true,false] (downside: not scalar)
  4. store one single integer index/count of the known units, e.g. 5 would indicate data is known to the nearest minute.
I would start with the fourth option (on the basis that this is by far the simplest) and see if that could work for your algorithm.
Russell Shomberg
Russell Shomberg on 19 Oct 2020
It's for a database that holds dates and times when certain events were observed. Sometimes those known to the second, but other times, they are only estimates and might only contain a year and month. I need to be able to store the datetimes for all of the, but also show which are estimates ISO 8601 says that this can be represented by just leaving the end digits off, but this causes MATLAB to assume they are all 1.
Users might want to pull of certain date ranges, or only for the estimates. Also if I was plotting them times, I would need to know which are estimates and by how much.
Siddharth Bhutiya
Siddharth Bhutiya on 23 Oct 2020
Based on this information, as you had mentioned in your earlier comment a custom class seems to be the way to go here. Your class can have 2 properties, a datetime and an 'estimate' metadata. What you choose as the datatype for your 'estimate' metadata would depend on your use case. For example, let's say, if the data is such that even if the time is provided, it still can be an estimate, i.e. it can be that time +/- x hrs, then it makes sense to store the 'estimate' as calendarDuration or duration. However, if the data is such that whatever is provided is accurate and only the missing pieces are unknown/estimates then you can store the 'estimate' as a scalar categorical that notes the last known unit ('day','month','hour',etc.), as Stephen mentioned above.
You mentioned that it is for a database, then maybe instead of complicating things by creating a class, you might be better off having 2 fields in your table one for the datetime and one for the 'estimate'.

Sign in to comment.

More Answers (0)


Find more on Dates and Time in Help Center and File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!