Andy Harvey
Andy Harvey

Reputation: 12653

How to parse variously formatted or natural language dates locally and pass to jquery datepicker?

I have a :date_as_text field in a Rails form. When the form is submitted this field is parsed by Chronic and a datetime saved to the database.

As this is a text field, the user can type a variety of different date formats, or even 'today', 'yesterday', etc and all (most) are interpreted by Chronic.

Input values can also be set via a jquery datepicker attached to the field.

If the user types an entry that corresponds exactly to the date format expected by datepicker (in this case yy-MM-dd), then the datepicker is updated to display the currently selected data.

How can I parse the entered value on the client side so that the datepicker will also recognize other date formats? I'm struggling to conceptualize how to even approach this.

  1. How to parse variously formatted or natural language dates locally? One approach seems to be https://github.com/datejs/Datejs, though this library does not appear to be well maintained.
  2. When to parse? x seconds after the last entry in the field? Or is there a better way?
  3. How to pass the parsed value into datepicker? Would the datepicker need to be reinitialized?

Does this sound feasible, or is a Really Bad Idea? Appreciate thoughts or experience from others.

Upvotes: 0

Views: 135

Answers (0)

Related Questions