Skip to content

Parses a given English date string, absolute or relative, to a Date object.

Notifications You must be signed in to change notification settings

alexc155/its-a-date

 
 

Repository files navigation

alt tag

When given a Date description, it returns a Date object. No need to know exact date format.

Currently supporting languages:

  • English
  • Russian
  • Arabic
  • Persian
  • Turkish
  • Greek
  • Spanish
  • French

Installation

$ npm install its-a-date --save

Code Example

var itsadate = require('its-a-date');

// Mon May 02 2016 05:48:00 GMT+0300 (Jerusalem Daylight Time)`
itsadate.parse('May 2 05:48:29 2016');

// Wed Apr 11 1990 00:45:00 GMT+0300 (Jerusalem Daylight Time)
itsadate.parse('3 months after 11/01/1990');

// Sat Aug 26 1989 02:43:00 GMT+0300 (Jerusalem Daylight Time)
itsadate.parse('10000 days before 11/01/2017');

More Code Examples

Parse Hour & Date

// Fri Aug 19 2016 11:47:00 GMT+0300 (Jerusalem Daylight Time)
itsadate.parse('11:47');

// Tue Mar 27 2012 14:05:00 GMT+0200 (Jerusalem Standard Time)
itsadate.parse('27.3.2012');

// Tue Mar 27 2012 12:00:00 GMT+0200 (Jerusalem Standard Time)
itsadate.parse('27/3/2012 12:00');

Parse Relatives

// Thu Aug 18 2016 14:05:00 GMT+0300 (Jerusalem Daylight Time)
itsadate.parse('yesterday');

// Thu Aug 18 2016 16:20:00 GMT+0300 (Jerusalem Daylight Time)
itsadate.parse('yesterday at 16:20');

//Fri Aug 19 2011 14:05:00 GMT+0300 (Jerusalem Daylight Time)
itsadate.parse('5 years ago');

// Fri Aug 19 2011 15:44:00 GMT+0300 (Jerusalem Daylight Time)
itsadate.parse('5 years ago at 15:44');

Parse the Extreme (Not Recommended)

When its-a-date doesn't know what date it is, it will return undefined. However many times it surprises.

// Sun Jan 01 1984 14:05:00 GMT+0200 (Jerusalem Standard Time)
itsadate.parse('it was on the 1st of January, ' + 'the dark cold winter of 1984');

// undefined
itsadate.parse('pikachu');

Settings, Formats & Time Differences

Use settings to alter the format expected

// Hint its-a-date to prefer mm/dd/yyyy over dd/mm/yyyy
itsadate.settings({ day_before_month: false });

// Thu Nov 01 1990 01:55:00 GMT+0200 (Jerusalem Standard Time)
itsadate.parse('11/1/1990');
// Don't allow its-a-date to guess any other format than specified (day MUST be before month or vise-versa)
itsadate.settings({ strict: true });

You can prevent changing the settings globaly, by delivering it as a second parameter

// November 3rd 2014
itsadate.parse('11-3-2014', { day_before_month: false });

// Doesn't use prev settings March 11th 2014
itsadate.parse('11-3-2014');

Restore settings to default

// Restore setting back to default (day_before_month=true)
itsadate.settings().restore();

// Thu Jan 11 1990 01:55:00 GMT+0200 (Jerusalem Standard Time)
itsadate.parse('11/1/1990');

Alter GMT to overcome time difference

// Set GMT+5.5, Mumbai India
itsadate.settings({ gmt: 5.5 });
itsadate.parse('now');

Change the base date that its-a-date uses to 'fill in' blanks

// Set base date to 31st December 1999
itsadate.settings({ base_date: new Date(1999, 11, 31) });

// Mon May 31 1999 00:00:00 GMT+0100 (British Summer Time)
itsadate.parse('May');

Learn More

Hit brag() to console out examples of any token implemented

itsadate.brag();

Hit settings() to get settings object with configutations you can alter. descriptions included.

itsadate.settings();

No Exceptions

When date description has inner conflicts it resolves to undefined

// undefined
itsadate.parse('1st of november 02/03/2000');

Motivation

  1. Allows a user to type a date more naturally in a web page or social media.
  2. Allows crawlers to parse relative dates easily.

Strategy (How it works?)

A micro-compiler is implemented. It parses date part tokens & interpret them as modifications on the perceived date.

Upcoming Features

  1. Custom token insertion
  2. More Languages perhaps

Git

https://github.com/bendorshai/its-a-date

About

Parses a given English date string, absolute or relative, to a Date object.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%