Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 228115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:39:18+00:00 2026-05-11T19:39:18+00:00

I am implementing a Fuzzy Date control in C# for a winforms application. The

  • 0

I am implementing a Fuzzy Date control in C# for a winforms application. The Fuzzy Date should be able to take fuzzy values like

  • Last June
  • 2 Hours ago
  • 2 Months ago
  • Last week
  • Yesterday
  • Last year

and the like

Are there any sample implementations of “Fuzzy” Date Time Pickers?

Any ideas to implement such a control would be appreciated

PS:
I am aware of the fuzzy date algorithm spoken about here and here, I am really looking for any ideas and inspirations for developing such a control

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:39:18+00:00Added an answer on May 11, 2026 at 7:39 pm

    The parsing is quite easy. It can be implemented as bunch of regexps and some date calculations.

    The sample below can be easily extended to suit your needs.
    I’ve roughly tested it and it works at least for the following strings:

    • next month, next year,
    • next 4 months, next 3 days
    • 3 days ago, 5 hours ago
    • tomorrow, yesterday
    • last year, last month,
    • last tue, next fri
    • last june, next may,
    • jan 2008, 01 january 2009,
    • june 2019, 2009/01/01

    The helper class:

    class FuzzyDateTime
    {
    
        static List<string> dayList = new List<string>() { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
        static List<IDateTimePattern> parsers = new List<IDateTimePattern>()
        {
           new RegexDateTimePattern (
                @"next +([2-9]\d*) +months",
                delegate (Match m) {
                    var val = int.Parse(m.Groups[1].Value); 
                    return DateTime.Now.AddMonths(val);
                }
           ),
           new RegexDateTimePattern (
                @"next +month",
                delegate (Match m) { 
                    return DateTime.Now.AddMonths(1);
                }
           ),           
           new RegexDateTimePattern (
                @"next +([2-9]\d*) +days",
                delegate (Match m) {
                    var val = int.Parse(m.Groups[1].Value); 
                    return DateTime.Now.AddDays(val);
                }
           ),
    
           new RegexDateTimePattern (
                @"([2-9]\d*) +months +ago",
                delegate (Match m) {
                    var val = int.Parse(m.Groups[1].Value); 
                    return DateTime.Now.AddMonths(-val);
                }
           ),
           new RegexDateTimePattern (
                @"([2-9]\d*) days +ago",
                delegate (Match m) {
                    var val = int.Parse(m.Groups[1].Value); 
                    return DateTime.Now.AddDays(-val);
                }
           ),
           new RegexDateTimePattern (
                @"([2-9]\d*) *h(ours)? +ago",
                delegate (Match m) {
                    var val = int.Parse(m.Groups[1].Value); 
                    return DateTime.Now.AddMonths(-val);
                }
           ),
           new RegexDateTimePattern (
                @"tomorrow",
                delegate (Match m) {
                    return DateTime.Now.AddDays(1);
                }
           ),
           new RegexDateTimePattern (
                @"today",
                delegate (Match m) {
                    return DateTime.Now;
                }
           ),
           new RegexDateTimePattern (
                @"yesterday",
                delegate (Match m) {
                    return DateTime.Now.AddDays(-1);
                }
           ),
           new RegexDateTimePattern (
                @"(last|next) *(year|month)",
                delegate (Match m) {
                    int direction = (m.Groups[1].Value == "last")? -1 :1;
                    switch(m.Groups[2].Value) 
                    {
                        case "year":
                            return new DateTime(DateTime.Now.Year+direction, 1,1);
                        case "month":
                            return new DateTime(DateTime.Now.Year, DateTime.Now.Month+direction, 1);
                    }
                    return DateTime.MinValue;
                }
           ),
           new RegexDateTimePattern (
                String.Format(@"(last|next) *({0}).*", String.Join("|", dayList.ToArray())), //handle weekdays
                delegate (Match m) {
                    var val = m.Groups[2].Value;
                    var direction = (m.Groups[1].Value == "last")? -1 :1;
                    var dayOfWeek = dayList.IndexOf(val.Substring(0,3));
                    if (dayOfWeek >= 0) {
                        var diff = direction*(dayOfWeek - (int)DateTime.Today.DayOfWeek);
                        if (diff <= 0 ) { 
                            diff = 7 + diff;
                        }
                        return DateTime.Today.AddDays(direction * diff);
                    }
                    return DateTime.MinValue;
                }
           ),
    
           new RegexDateTimePattern (
                @"(last|next) *(.+)", // to parse months using DateTime.TryParse
                delegate (Match m) {
                    DateTime dt;
                    int direction = (m.Groups[1].Value == "last")? -1 :1;
                    var s = String.Format("{0} {1}",m.Groups[2].Value, DateTime.Now.Year + direction);
                    if (DateTime.TryParse(s, out dt)) {
                        return dt;
                    } else {
                        return DateTime.MinValue;
                    }
                }
           ),
           new RegexDateTimePattern (
                @".*", //as final resort parse using DateTime.TryParse
                delegate (Match m) {
                    DateTime dt;
                    var s = m.Groups[0].Value;
                    if (DateTime.TryParse(s, out dt)) {
                        return dt;
                    } else {
                        return DateTime.MinValue;
                    }
                }
           ),
        };
    
        public static DateTime Parse(string text)
        {
            text = text.Trim().ToLower();
            var dt = DateTime.Now;
            foreach (var parser in parsers)
            {
                dt = parser.Parse(text);
                if (dt != DateTime.MinValue)
                    break;
            }
            return dt;
        }
    }
    interface IDateTimePattern
    {
        DateTime Parse(string text);
    }
    
    class RegexDateTimePattern : IDateTimePattern
    {
        public delegate DateTime Interpreter(Match m);
        protected Regex regEx;
        protected Interpreter inter;
        public RegexDateTimePattern(string re, Interpreter inter)
        {
            this.regEx = new Regex(re);
            this.inter = inter;
        }
        public DateTime Parse(string text)
        {
            var m = regEx.Match(text);
    
            if (m.Success)
            {
                return inter(m);
            }
            return DateTime.MinValue;
        }
    }
    

    Usage example:

    var val = FuzzyDateTime.Parse(textBox1.Text);
    if (val != DateTime.MinValue)
       label1.Text = val.ToString();
    else
       label1.Text = "unknown value";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 105k
  • Answers 105k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The HTML and CSS would be useful to answer this.… May 11, 2026 at 8:42 pm
  • Editorial Team
    Editorial Team added an answer If you really insist on hiding the ORM layer behind… May 11, 2026 at 8:42 pm
  • Editorial Team
    Editorial Team added an answer My first advice: Skip this $Include thing altogether. As Uwe… May 11, 2026 at 8:42 pm

Related Questions

I am implementing a HttpRequestValidationException in my Application_Error Handler, and if possible, I want
I am implementing a quite simple state-machine order processing application. It is a e-commerce
I am implementing a comment control that allows a person to select comments and
I am implementing a design that uses custom styled submit-buttons. They are quite simply
I am implementing a very simple file database. I have 2 basic operations: void

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.