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

  • SEARCH
  • Home
  • 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 7682651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:37:01+00:00 2026-05-31T18:37:01+00:00

In looking at Fuzzy Date Time Picker Control in C# .NET? Piotr Czaapla’s answer

  • 0

In looking at Fuzzy Date Time Picker Control in C# .NET? Piotr Czaapla’s answer to that question is exactly what I need.

Unfortunately, I’m a VB.NET guy and I’m not that familiar with lambda expresions, so my attempts to convert the code have resulted in hours of misplaced parenthesis and banging my head with a brick.

Any chance some bi-lingual wizard could convert the C# code to VB.net for me?

Here’s the code in question:

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;
    }
}
  • 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-31T18:37:03+00:00Added an answer on May 31, 2026 at 6:37 pm

    OK, there’s a bunch of code there, I’m not going to translate it all but for example (assuming VS2010 or later) . . .

    new RegexDateTimePattern (
        @"next +([2-9]\d*) +months",
        delegate (Match m) {
            var val = int.Parse(m.Groups[1].Value); 
            return DateTime.Now.AddMonths(val);
        }
    )
    

    will be

        Dim p2 = New RegexDateTimePattern(
        "next +([2-9]\d*) +months",
        Function(m)
            Dim val = Int.Parse(m.Groups(1).Value)
            Return DateTime.Now.AddMonths(val)
        End Function
        )
    

    and

    new RegexDateTimePattern (
        String.Format(@"(last|next) *({0}).*", String.Join("|", dayList.ToArray())), 
        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;
        }
    ),
    

    becomes

        Dim p = New RegexDateTimePattern(
           String.Format("(last|next) *({0}).*", String.Join("|", dayList.ToArray())),
           Function(m)
               Dim val = m.Groups(2).Value
               Dim direction = If(m.Groups(1).Value = "last", -1, 1)
               Dim dayOfWeek = dayList.IndexOf(val.Substring(0, 3))
               If (dayOfWeek >= 0) Then
                   Dim diff = direction * (dayOfWeek - CType(DateTime.Today.DayOfWeek, Integer))
                   If (diff <= 0) Then
                       diff = 7 + diff
                   End If
                   Return DateTime.Today.AddDays(direction * diff)
               End If
               Return DateTime.MinValue
           End Function
           )
    

    Is there anything else in there that’s twisting your mellon?

    If it’s not VS2010 (or even if it is and you don’t like the lambda expressions), then all you need to do is take each of the lambda expressions (the “in line” functions in the VB code), create explicit named functions for each one (e.g. RegexDateTimePattern_Helper2 for the last one), and pass AddressOf RegexDateTimePattern_Helper2 instead of the lambda expression

    i.e.

        Function RegexDateTimePattern_Helper2(Match m) as DateTime ''# Dreadful name
            dim val = m.Groups[2].Value
            dim direction = if(m.Groups(1).Value = "last", -1 ,1)
            Dim dayOfWeek = dayList.IndexOf(Val.Substring(0, 3))
            If (dayOfWeek >= 0) Then
                Dim diff = direction * (dayOfWeek - CType(DateTime.Today.DayOfWeek, Integer))
                If (diff <= 0) Then
                    diff = 7 + diff
                End If
                Return DateTime.Today.AddDays(direction * diff)
            End If
            Return DateTime.MinValue
        End Function
    

    .
    .
    .

    New RegexDateTimePattern (
        String.Format("(last|next) *({0}).*", String.Join("|", dayList.ToArray())), 
        AddressOf RegexDateTimePattern_Helper2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a fuzzy date algorithm. I just started writing one and realised
I would like to have fuzzy looking border around my Canvas control. Basically, I
I am implementing a Fuzzy Date control in C# for a winforms application. The
I'm looking for a Python module that can do simple fuzzy string comparisons. Specifically,
Looking for a build-time CSS combiner/minifier that respects relative URL references. That is, if
I'm looking for a fuzzy search implementation that works well with western European languages.
Looking for an example that: Launches an EXE Waits for the EXE to finish.
Looking for a Linux application (or Firefox extension) that will allow me to scrape
I am looking for something like Textmate's fuzzy search on Command-T, FuzzyFinder in Vim,
I'm looking to implement fuzzy search for a small PHP/MySQL application. Specifically, I have

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.