Code Monkey home page Code Monkey logo

chrono's Introduction

Chrono

A natural language date parser in Javascript.

INSTALLATION

Node.js

npm install chrono-node

Browser

component install

USAGE

Basic

You can parse strings containing a natural language date using the chrono.parseDate or chrono.parse method.

> var chrono = require('chrono-node');

> chrono.parseDate('Today'); 
Thu Aug 23 2012 12:00:00 GMT+0700 (ICT)

> chrono.parse('Today'); 
[ { start: 
     { year: 2012,
       month: 7,
       day: 23,
       hour: undefined,
       minute: undefined,
       second: undefined,
       date: [Function] },
    startDate: Thu Aug 23 2012 12:00:00 GMT+0700 (ICT),
    referenceDate: Thu Aug 23 2012 01:17:46 GMT+0700 (ICT),
    index: 0,
    text: 'Today',
    sentence: undefined } ]

Reference Date

Today's "Friday" is difference from last month's "Friday". The meaning of mentioned dates are depended on when they were mentioned. Chrono let you define the reference date using chrono.parse(text,ref) and chrono.parseDate(text,ref).

> chrono.parseDate('Friday', new Date(2012,7,23)); 
Fri Aug 24 2012 12:00:00 GMT+0700 (ICT)

> chrono.parseDate('Friday', new Date(2012,7,1)); 
Fri Aug 03 2012 12:00:00 GMT+0700 (ICT)

Text

Chrono is also designed to handle a long text. chrono.parse will return a array of every date mentioned in the input text. chrono.parseDate will return the first one.

> var text = 'October 7, 2011, of which details were not revealed out of respect to Jobs\'s family.[239] Apple announced on the same day that they had no plans for a public service, but were encouraging "well-wishers" to send their remembrance messages to an email address created to receive such messages.[240] Sunday, October 16, 2011'

> chrono.parse(text)
[ { start: 
     { year: 2011,
       month: 9,
       day: 7,
       hour: undefined,
       minute: undefined,
       second: undefined,
       date: [Function] },
    startDate: Fri Oct 07 2011 12:00:00 GMT+0700 (ICT),
    referenceDate: Thu Aug 23 2012 01:48:37 GMT+0700 (ICT),
    index: 0,
    text: 'October 7, 2011',
    sentence: undefined },
  { start: 
     { year: 2011,
       month: 9,
       day: 16,
       hour: undefined,
       minute: undefined,
       second: undefined,
       date: [Function] },
    startDate: Sun Oct 16 2011 12:00:00 GMT+0700 (ICT),
    referenceDate: Thu Aug 23 2012 01:48:37 GMT+0700 (ICT),
    index: 297,
    text: 'Sunday, October 16, 2011',
    sentence: undefined } ]

EXAMPLES

Chrono can parse a number of date and time formats. Following are examples of strings that can be parsed properly.

  • yesterday
  • today
  • tomorrow
  • thursday
  • friday 13:00
  • friday 1pm - saturday 5am
  • next tuesday
  • last night
  • last friday at 20:00
  • tomorrow at 6:45pm
  • 12/1/2012
  • 12 - 13 Jan 2012
  • 12 Jan - 13 Feb 2012
  • Jan 12 - Feb 13
  • 2012-3-12

chrono's People

Contributors

lachenmayer avatar wanasit avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.