40

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

2
  • Very similar to your other post stackoverflow.com/questions/1003330/… Why not just say javascript or Cold Fusion? Commented Jun 16, 2009 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? Commented Jun 19, 2009 at 17:16

7 Answers 7

40

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') .

1
  • 5
    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.
    Commented Oct 20, 2014 at 1:31
28

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.

2
  • 3
    SugarJS is better, especially when handling cases that include both date and time.
    – Haozhun
    Commented Oct 6, 2012 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. Commented May 2, 2013 at 14:07
9

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

7
  • 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. Commented Jun 16, 2009 at 19:09
  • What scares you, the extension of native objects? Or something else?
    – Nosredna
    Commented Jun 16, 2009 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. Commented Jun 16, 2009 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
    Commented Jun 16, 2009 at 20:17
  • 1
    Date.js official Google Code datejs.com/googlecode is no longer found - looks dead to me. Commented Jun 24, 2014 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

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
2
  • This is a duplicate of Wanasit Tanakitrungruang's answer. Commented Dec 19, 2019 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
    Commented Jun 28, 2022 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
3
  • 2
    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
    Commented Jun 28, 2022 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 for details. it probably has timezone included but it's better to ask the library author :) Commented Jun 30, 2022 at 7: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 myself :)
    – Prid
    Commented Jun 30, 2022 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];
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
    Commented Aug 4, 2014 at 11:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.