0

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.

Andy Harvey
  • 12,333
  • 17
  • 93
  • 185
  • 1
    Conceptually I dont think its a good idea to have a text field for a date that accepts different formats. It can seriously confuse user. Much better to have a field with mask or something that accepts only certain format and if you need you can always add button like 'today' but it seems redundant if you have datepicker there. – Dmitriy Nevzorov Mar 19 '16 at 12:51
  • What you trying to achieve creates lots of edge cases Like handling errors when date string is not recognised and notifying user what formats he could use in this field. – Dmitriy Nevzorov Mar 19 '16 at 12:53
  • I think using two different NLP's is not a good idea (client side and server side). Better to pass it to server on calendar icon click and show some loading icon while its process.. before you activate the calendar.. I think you need to tweak the date picker plugin to support this functionality. – JChap Mar 19 '16 at 13:15

0 Answers0