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

Go To StackoverFlow.com

23

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

2009-06-16 18:54
by antony.trupe
Very similar to your other post

http://stackoverflow.com/questions/1003330/is-there-a-natural-language-parser-for-date-times-in-coldfusion

Why not just say javascript or Cold Fusion - samoz 2009-06-16 19:02

One is a client side solution, one is a server side. I felt that trying to combine them would result in 3 questions: 1. Which is better? 2. What's the best client side solution? 3.Whats the best server side solution - antony.trupe 2009-06-19 17:16


23

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.

2011-10-03 23:44
by bradhouse
SugarJS is better, especially when handling cases that include both date and time - Haozhun 2012-10-06 15:32
I completely agree with @Haozhun. I've used MomentJS, DateJS and SugarJS before but IMO the latter is by far the best at NLP - Ryan Brodie 2013-05-02 14:07


22

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.

2012-11-28 15:48
by Wanasit Tanakitrungruang
You have the best NLP for time I've found so far in JavaScript. So far rank would be: #1 chrono, #2 sugarjs, #3 datejs. Tip: I suggest you rename it to Chronojs to give it a more searchable and distinctive name - DG. 2014-10-20 01:31


9

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

2009-06-16 18:56
by Nosredna
I found that, but the source code scares me, on multiple levels. I was hoping for something that integrates with one of the 'modern' javascript libraries - antony.trupe 2009-06-16 19:09
What scares you, the extension of native objects? Or something else - Nosredna 2009-06-16 19:43
on the surface, the lack of formatting and/or lack of a development version and production version. Its also not an active project, which may not be relevant to everyone - antony.trupe 2009-06-16 20:12
I've never seen anything close to it, though, especially all the localization. Let us know if you find something else. Maybe someone should take it over as a project - Nosredna 2009-06-16 20:17
There were changes made in svn in late 2008, so it's not TOO far out of date - Nosredna 2009-06-16 20:20
Date.js extends the native Date object in a way that does not jive with jasmine's time mocking mechanism jasmine.Clock.useMock() and jasmine.Clock.tick(123 - GregT 2012-04-07 23:29
Date.js official Google Code http://www.datejs.com/googlecode/ is no longer found - looks dead to me - mikemaccana 2014-06-24 10:11


2

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
2018-07-02 13:39
by student


-5

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];
2011-12-28 16:29
by Karim
This doesn’t answer the question. OP is asking for a parser, that is, a function that takes a string like '23/02/1945' or 'next monday' and returns a Date object - bfontaine 2014-08-04 11:46
Ads