6

I'm trying to let users type in date and time values into an input field. Some examples may include:

Tomorrow at 3pm
Now
Today at 1pm
Next tuesday 4pm

How would I go about parsing such readable text into date/time strings?

E.g. dd/mm/yyyy hh:mm

Can it be done client-side using jquery or must it be done ajax through php?

Thanks in advance for any advice or insights :)

Lyon
  • 7,354
  • 10
  • 33
  • 46

4 Answers4

6

There is an open source javascript library doing all that funky stuff with dates. You can check out http://www.datejs.com/

Sinan
  • 5,819
  • 11
  • 39
  • 66
3

Though I vote for datejs.com, another option is timeapi.org.

timeapi.org has a JSONP API for converting natural language queries to date/time.

vsr
  • 3,138
  • 1
  • 19
  • 14
0

Thanks Sinan and vsr. I voted up for both your answers. I was initially very excited with what datejs has to offer, but it seems limited in its ability to process natural language date time. It doesn't handle date + time very well.

timeapi.org looks promising, but as it utilises ruby, and the api version relies on a service that does not appear to be very reliable.

I tested with php's strtotime and found that it could process more natural language date time pretty well. Using php allows me to add other natural language terms not supported too. So it looks like I'll be adopting ajax to process the natural language date time from the user.

Would love to hear your insights about my approach. :) thanks again!

Lyon
  • 7,354
  • 10
  • 33
  • 46
0

I think this is a duplicate of Is there a natural language parser for date/times in javascript?

The answer I've just given for that question is below:

SugarJS supports some natural language parsing of dates and times.

You can jump to the live example here: http://sugarjs.com/dates

For example, it supports the following inputs:

  • the day after tomorrow
  • 2 weeks from monday
  • May 25th of next year

You can then covert the result into different date formats or use the API to further manipulate the date.

Community
  • 1
  • 1
Bradley Dwyer
  • 8,102
  • 5
  • 32
  • 28