27

I have a system that takes information from an external source and then stores it to be displayed later.

One of the data items is a date. On the source system they have the concept of a fuzzy date i.e. not accurate to a specific day or sometimes not to a month as well. So I get dates in the format:

dd/mm/yyyy
mm/yyyy
yyyy

I can parse these to DateTime objects and work with these but when rendering later I need to be able to determine the accuracy of the date since parsing "2010" will result in a date of "01/01/2010". I want to show just the year so need to know it's original accuracy.

I've mocked up a quick class to deal with this:

public class FuzzyDate
{
    public DateTime Date { get; set; }
    public DateType Type { get; set; }
}

public enum DateType
{
    DayMonthYear,
    MonthYear,
    Year
}

This will do the job for me and I can do something on the parse to handle it but I feel like this is probably quite a common problem and there is probably an existing cleaner solution.

Is there something built into .Net to do this? I had a look at the culture stuff but that didn't quite seem right.

Any help would be appreciated.

2
  • I don't know of anything built in to support this. I would recommend taking a look at NodaTime (code.google.com/p/noda-time) - though I don't see anything in browsing that source that specifically solves your problem but you might. Also consider the possibility of storing the string format representation (or a reference to a format) instead of an enum value - this allows you to add new date storage formats if the requirements change without recompiling and redeploying.
    – cfeduke
    Jul 27, 2011 at 16:12
  • possible duplicate of Implementing a "Partial Date" object Jul 27, 2011 at 16:48

8 Answers 8

14

To answer your question: There is nothing built into .NET to handle this gracefully.

Your solution is as valid as any I've seen. You will probably wish to embellish your class with overrides to the ToString() method that will render your date appropriately based on the DateType.

Here are a couple other threads that attempt to address this question:

Good luck!

7

If your data type will always handle specific periods of time (i.e. the year 1972 is a specific period of time, but the 4th of July is not specific), you can store your data as a start time and time span.

  • If your date was "1972", the start date would be 19720101 and the time span would be 366 days.
  • If your date was "07/1972", the start date would be 19720701 and the time span would be 31 days.
  • If your date was "04/07/1972", the start date would be 19720704 and the time span would be 1 day.

Here's a possible implementation:

public struct VagueDate
{
    DateTime start, end;

    public DateTime Start { get { return start; } }
    public DateTime End { get { return end; } }
    public TimeSpan Span { get { return end - start; } }

    public VagueDate(string Date)
    {
        if (DateTime.TryParseExact(Date, "yyyy", null, 0, out start))
            end = start.AddYears(1);
        else if (DateTime.TryParseExact(Date, "MM/yyyy", null, 0, out start))
            end = start.AddMonths(1);
        else if (DateTime.TryParseExact(Date, "dd/MM/yyyy", null, 0, out start))
            end = start.AddDays(1);
        else
            throw new ArgumentException("Invalid format", "Date");
    }

    public override string ToString()
    {
        return Start.ToString("dd/MM/yyyy") + " plus " + Span.TotalDays + " days";
    }
}
6

As I started to read your problem, I rapidly came to the conclusion that the answer was to implement your own FuzzyDate class. Lo and behold, that's exactly what you've done.

I can imagine that you might want to add functionality to this over time (such as comparisons that take into account the DateType).

I don't believe there's anything that will inherently help you in the .NET Framework, so I think you're doing the right thing.

3

I think you're going down the right route. There is no concept of a 'fuzzy' date or partial date, you will need to build your own.

You will likely need more constructor methods, for example

public FuzzyDate(int year)
{
   Date = new DateTime(year,1,1); // 1 Jan yy
   Type = DateType.Year;
}

public FuzzyDate(int year, int month)
{
   Date = new DateTime(year, month, 1); // 1 mm yy
   Type = DateType.MonthYear;
}

public FuzzyDate(int year, int month, int day)
{
   Date = new DateTime(year, month, day); // dd mm yy
   Type = DateType.DayMonthYear;
}

Hope this helps, Kevin

1

It seems to me that your approach is right. Its true that .NET DateTime support multiple formats but I guess that given that all of them are supported with a concept of steps (nanoseconds), then will be related to specific date AND time.

0

One thing I would do differently is use null-able values (or use -1 for null semantics) for month and day to indicate what data was collected. Then I would have a factory method that would take a DateType param and return a DateTime. This method would throw and exception if only the year was available and the client code tried to create a DateType.DayMonthYear.

public class FuzzyDate
{
    int _year;
    int? _month;
    int? _day;

    public DateTime Date { get; set; }
    public DateType Type { get; set; }

    public DateTime GetDateTime(DateType dateType) { // ...
}

public enum DateType
{
    DayMonthYear,
    MonthYear,
    Year
}

This might seem a bit over the top but the approach would explicitly store the original data and only represent "faked" DateTime objects when requested. If you were to persist a DateTime object internally along with a DateType enum you would lose some resolution.

4
  • I have a hard time picturing having a year and a day but no month. I prefer the OP's approach of the DateTypes enum
    – Neil N
    Jul 27, 2011 at 16:28
  • For that matter, I can more readily imagine wanting to store a month and day with no year: to store a person's birthdate without knowing their age, for example. "Day" in this context is day-of-month, and doesn't really have any meaning without it.
    – Dan J
    Jul 27, 2011 at 16:36
  • @Neil: The OP's approach is fine. My concern was storing explicitly the state of data at time of capture which you lose once you attach a flag to a hacked DateTime value.
    – Paul Sasik
    Jul 27, 2011 at 17:36
  • @djacobson: The fuzzy date issue is actually quite common in a lot of domains. For example, persons in developing countries often only know the year of their birth but not the month or day. Sometimes they don't even know that and an approximation has to be recorded. In that case it's good to store extra info with your fuzzy date object with boolean flags such as Approximate and GovernmentIdValidated...
    – Paul Sasik
    Jul 27, 2011 at 17:39
0

As far as I am aware there is nothing built into .NET for this, the solution I'd go for is one based upon nullable values, something like this.

public class FuzzyDate
{
    private int Year;
    private int? Month;
    private int? Day;

    public FuzzyDate(int Year, int? Month, int? Day)
    {
        this.Year = Year;
        this.Month = Month;
        this.Day = Day;
    }

    public DateType DateType
    {
        get
        {
            if(Day.HasValue && Month.HasValue)
            {
                return DateType.DayMonthYear;
            }
            else if(Month.HasValue)
            {
                return DateType.MonthYear;
            }
            else
            {
                return DateType.Year;
            }
        }
    }

    public DateTime Date
    {
        get
        {
            return new DateTime(Year, Month.GetValueOrDefault(1), Day.GetValueOrDefault(1));
        }
    }
}

public enum DateType
{
    DayMonthYear,
    MonthYear,
    Year
}
0

You could create your own structure (user-defined type) based on the datetime that would allow 00 for month, and 00 for day... And then also implement icomparable, so you can do math/comparrisons on it.

http://msdn.microsoft.com/en-us/library/k69kzbs1%28v=vs.71%29.aspx
http://msdn.microsoft.com/en-us/library/system.icomparable.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.