<p>This question is the other side of the question asking, "<a href="https://stackoverflow.com/questions/11/how-do-i-calculate-relative-time">How do I calculate relative time?</a>".</p>

<p>Given some human input for a relative time, how can you parse it?  By default you would offset from <code>DateTime.Now()</code>, but could optionally offset from another <code>DateTime</code>.</p>

<p>(Prefer answers in C#)</p>

<p>Example input:</p>

<ul>
<li>"in 20 minutes"</li>
<li>"5 hours ago"</li>
<li>"3h 2m"</li>
<li>"next week"</li>
</ul>

<p><strong>Edit:</strong>  Let's suppose we can define some limits on the input.  This sort of code would be a useful thing to have out on the web.</p>

[{'answer_id': 55445, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 3, 'selected': True, 'text': '<p>That\'s building a DSL (Domain specific language) for date handling. I don\'t know if somebody has done one for .NET but the construction of a DSL is fairly straightforward:</p>\n\n<ol>\n<li>Define the language precisely, which input forms you will accept and what will you do with ambiguities</li>\n<li>Construct the grammar for the language</li>\n<li>Build the finite state machine that parses your language into an actionable AST</li>\n</ol>\n\n<p>You can do all that by yourself (with the help of <a href="http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools" rel="nofollow noreferrer">the Dragon Book</a>, for instance) or with the help of tools to the effect, as shown in this <a href="http://www.codeproject.com/KB/recipes/YourFirstDSL.aspx" rel="nofollow noreferrer">link</a>.</p>\n\n<p>Just by thinking hard about the possibilities you have a good chance, with the help of good UI examples, of covering more than half of the actual inputs your application will receive. If you aim to accept everything a human could possibly type, you can record the input determined as ambiguous and then add them to the grammar, whenever they can be interpreted, as there are things that will be inherently ambiguous.</p>\n'}, {'answer_id': 55465, 'author': 'GEOCHET', 'author_id': 5640, 'author_profile': 'https://Stackoverflow.com/users/5640', 'pm_score': -1, 'selected': False, 'text': '<p>This: <a href="http://www.codeproject.com/KB/edit/dateparser.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/edit/dateparser.aspx</a>\nIs fairly close to what you are trying to accomplish. Not the most elegant solution, but certainly might save you some work.</p>\n'}, {'answer_id': 55469, 'author': 'dF.', 'author_id': 3002, 'author_profile': 'https://Stackoverflow.com/users/3002', 'pm_score': 3, 'selected': False, 'text': '<p>A Google search turns up the <a href="http://code.google.com/p/parsedatetime/" rel="nofollow noreferrer">parsedatetime</a> library (associated with the <a href="http://chandlerproject.org/" rel="nofollow noreferrer">Chandler project</a>), which is designed to do exactly this. It\'s open source (Apache License) and written in Python. It seems to be quite sophisticated -- from the homepage:</p>\n\n<blockquote>\n  <p>parsedatetime is able to parse, for\n  example, the following:</p>\n\n<pre><code>* Aug 25 5pm\n* 5pm August 25\n* next saturday\n...\n* tomorrow\n* next thursday at 4pm\n* at 4pm\n* eod\n* in 5 minutes\n* 5 minutes from now\n* 5 hours before now\n* 2 days from tomorrow\n</code></pre>\n</blockquote>\n\n<p>Since it\'s implemented in pure Python and doesn\'t use anything fancy, there\'s a good chance it\'s compatible with <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython" rel="nofollow noreferrer">IronPython</a>, so you could use it with .net. If you want specifically a C# solution, you could write something based on the algorithms they use...</p>\n\n<p>It also comes with a whole bunch of unit tests.</p>\n'}, {'answer_id': 55474, 'author': 'Jason Navarrete', 'author_id': 3920, 'author_profile': 'https://Stackoverflow.com/users/3920', 'pm_score': 0, 'selected': False, 'text': '<p>The ruby folks have attempted to tackle this with a parser called <strong>Chronic</strong>.</p>\n\n<ul>\n<li><a href="http://chronic.rubyforge.org/" rel="nofollow noreferrer">Chronic RDocs</a></li>\n<li><a href="http://github.com/mojombo/chronic/tree/master" rel="nofollow noreferrer">Chronic on GitHub</a></li>\n</ul>\n\n<p>I watched an informative video presentation recently on how the author went about solving this problem.  </p>\n\n<ul>\n<li><a href="http://podcast.sdruby.com/2006/10/3/episode-008-chronic" rel="nofollow noreferrer">Chronic Presentation (San Diego Ruby Brigade)</a></li>\n</ul>\n'}, {'answer_id': 55479, 'author': 'George Mauer', 'author_id': 5056, 'author_profile': 'https://Stackoverflow.com/users/5056', 'pm_score': 0, 'selected': False, 'text': '<p>This is likely not all that helpful since you\'re talking c# but since no one\'s mentioned it yet you can try to take a look at php\'s excellent and utterly insane native <a href="http://us2.php.net/strtotime" rel="nofollow noreferrer">strtotime</a> function</p>\n'}]
['https://Stackoverflow.com/questions/55434', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/337/']