Quantcast
Viewing all articles
Browse latest Browse all 7

thx: parsing dates

One of the latest additions to thx is thx.dates.DateParser. It is obviously a date parser, the nice thing about it is that it tries to be fuzzy and to extract a date whenever possible. You can try it yourself here:

Examples of expressions:

  • today
  • now
  • tomorrow
  • next april
  • last sunday
  • three weeks ago plus 5 minutes
  • tomorrow in the evening
  • 6/27
  • 27/6

To produce this tiny demo you just need a bunch of lines of code:

import thx.date.DateParser;
import thx.js.Dom;
import js.Dom;
class Main 
{
  static function main() 
  {
    Dom.select("#date").onNode("keyup", function(n : HtmlDom, _) {
      var t : Text = cast n;
      var result = try {
        Dates.format(DateParser.parse(t.value), "DT");
      } catch (e : Dynamic) {
        Std.string(e);
      }
      Dom.select("#processed")
        .html().string(result);
    });
  }
}

Surely the expression grammar can be improved but it works for my needs right now and it is already very flexible.


Viewing all articles
Browse latest Browse all 7

Trending Articles