Question

Is there a natural language parser for date/times in javascript?

Was it helpful?

Solution

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.

OTHER TIPS

I've developed a small library called chrono for parsing date in Javascript too. I also add date range parsing feature (such as '12 Nov - 13 Dec 2012')

You can check in here.

Does Date.js satisfy your needs? Or are you looking for something else?

For node, I've found chrono to work well

Chrono supports most date and time formats, such as :

  • Today, Tomorrow, Yesterday, Last Friday, etc
  • 17 August 2013 - 19 August 2013
  • This Friday from 13:00 - 16.00
  • 5 days ago
  • 2 weeks from now
  • Sat Aug 17 2013 18:40:39 GMT+0900 (JST)
  • 2014-11-30T08:15:30-05:30

You can use the jQuery datepicker translation, get the day and month number and select the day from datepicker days.

You can add values to this object, and you can download up to 60 languages I think. (The object below is not complete, I removed some code to simplify it).

$.datepicker.regional['sv'] = { 
  monthNames:['Januari','Februari','Mars','April','Maj','Juni','Juli','Augusti','September','Oktober','November','December'],
  monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec'],
  dayNamesShort: ['Sön','Mån','Tis','Ons','Tor','Fre','Lör'],
  dayNames: ['Söndag','Måndag','Tisdag','Onsdag','Torsdag','Fredag','Lördag'],
  dayNamesMin: ['Sö','Må','Ti','On','To','Fr','Lö']
};

Now get the day and month number

var dateObject = new Date();
var day = dateObject.getDay();
var month = dateObject.getMonth();
var monthText = $.datepicker.regional['sv']['monthNames'][month];
var dayText = $.datepicker.regional['sv']['dayNames'][day];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top