38

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

antony.trupe
  • 10,640
  • 10
  • 57
  • 84
  • 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 Jun 16 '09 at 19:02
  • 2
    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 Jun 19 '09 at 17:16

7 Answers7

38

I made Chrono a small library for parsing dates in JavaScript. I added a date range parsing feature (such as '12 Nov - 13 Dec 2012') .

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • 4
    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. Oct 20 '14 at 01:31
26

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.

Bradley Dwyer
  • 8,102
  • 5
  • 32
  • 28
  • 3
    SugarJS is better, especially when handling cases that include both date and time. – Haozhun Oct 06 '12 at 15:32
  • 1
    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 May 02 '13 at 14:07
9

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

Nosredna
  • 83,000
  • 15
  • 95
  • 122
  • 2
    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 Jun 16 '09 at 19:09
  • What scares you, the extension of native objects? Or something else? – Nosredna Jun 16 '09 at 19:43
  • 1
    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 Jun 16 '09 at 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 Jun 16 '09 at 20:17
  • There were changes made in svn in late 2008, so it's not TOO far out of date. – Nosredna Jun 16 '09 at 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 Apr 07 '12 at 23:29
  • 1
    Date.js official Google Code http://www.datejs.com/googlecode/ is no longer found - looks dead to me. – mikemaccana Jun 24 '14 at 10:11
6

Chrono v2 is the only library I've found that also parses timezones.

Unfortunately, the only documented way of using it is via NPM (npm i chrono-node) or as ES6 Module (import * as chrono from 'chrono-node'), but I found a way to use the library with the traditional script tag approach for those interested.

Non-NPM / non-module usage:

  1. Include this script: https://www.unpkg.com/chrono-node/dist/bundle.js
  2. Use the global variable chrono in your script (available methods here)

E.g. chrono.parseDate('Tomorrow at 4 PM PST')

⚠ Note: I have not tested this extensively, so don't expect it to work flawlessly

Prid
  • 1,272
  • 16
  • 20
5

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
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
  • This is a duplicate of Wanasit Tanakitrungruang's answer. – mikemaccana Dec 19 '19 at 16:31
  • 1
    @mikemaccana: this answer provides 3 additional values: 1) it's more recent (2012 vs 2018) which is reassuring (project still alive/working, still good to use in the current JS landscape), 2) it's a (positive) opinion from a *user* instead of the developer (less bias), 3) it shows a variety of examples of chrono's capabilities/strengths :) – Prid Jun 28 '22 at 18:46
3

Sherlock is a great one.

var Sherlock = require('sherlockjs');
var sherlocked = Sherlock.parse('Homework 5 due next monday at 3pm');

// Basic properties
var title = sherlocked.eventTitle;    // 'Homework 5 due'
var startDate = sherlocked.startDate; // Date object pointing to next monday at 3pm
var endDate = sherlocked.endDate;     // null in this case, since no duration was given
var isAllDay = sherlocked.isAllDay;   // false, since a time is included with the event

// Example of an additional custom property added by Watson
var validated = sherlocked.validated; // true
deadcoder0904
  • 7,232
  • 12
  • 66
  • 163
  • 1
    Great library! I'm only missing parsing of timezones, bc "Homework 5 due next monday at 3pm PDT" doesn't detect the Pacific Daylight Timezone :/ – Prid Jun 28 '22 at 17:30
  • 1
    @Prid yeah, i also want something like that. superhuman email app does that but they built-it internally so it's a no-beuno for us. another one i really like is compromise. see [this issue](https://github.com/spencermountain/compromise/discussions/895) for details. it probably has timezone included but it's better to ask the library author :) – deadcoder0904 Jun 30 '22 at 07:14
  • 1
    Thanks for the info I found Chrono to fit my needs as it detects timezones in their simplest form (abbreviations), but it wasn't straightforward to use it without npm/module. I found a way and made an [answer](https://stackoverflow.com/a/72791768/3705191) myself :) – Prid Jun 30 '22 at 13:35
-6

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];
Kjuly
  • 34,476
  • 22
  • 104
  • 118
Karim
  • 1
  • 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 Aug 04 '14 at 11:46