Skip to content


Folders and files

Last commit message
Last commit date

Latest commit



69 Commits

Repository files navigation

when godoc

when is a natural language date/time parser with pluggable rules and merge strategies


  • tonight at 11:10 pm
  • at Friday afternoon
  • the deadline is next tuesday 14:00
  • drop me a line next wednesday at 2:25 p.m
  • it could be done at 11 am past tuesday

Check EN rules and tests of them, for more examples.

Needed rule not found? Open an issue with the case and it will be added asap.

How it works

Usually, there are several rules added to the parser's instance for checking. Each rule has its own borders - length and offset in provided string. Meanwhile, each rule yields only the first match over the string. So, the library checks all the rules and extracts a cluster of matched rules which have distance between each other less or equal to options.Distance, which is 5 by default. For example:

on next wednesday at 2:25 p.m.
   └──────┬─────┘    └───┬───┘
       weekday      hour + minute

So, we have a cluster of matched rules - "next wednesday at 2:25 p.m." in the string representation.

After that, each rule is applied to the context. In order of definition or in match order, if options.MatchByOrder is set to true(which it is by default). Each rule could be applied with given merge strategy. By default, it's an Override strategy. The other strategies are not implemented yet in the rules. Pull requests are welcome.

Supported Languages

  • EN - English
  • RU - Russian
  • BR - Brazilian Portuguese
  • ZH - Chinese
  • NL - Dutch


The project follows the official release workflow. It is recommended to refer to this resource for detailed information on the process.

To install the latest version:

$ go get


w := when.New(nil)

text := "drop me a line in next wednesday at 2:25 p.m"
r, err := w.Parse(text, time.Now())
if err != nil {
	// an error has occurred
if  r == nil {
 	// no matches found

	"the time",
	"mentioned in",

Distance Option

w := when.New(nil)

text := "February 23, 2019 | 1:46pm"

// With default distance (5):
// February 23, 2019 | 1:46pm
//            └───┬───┘
//           distance: 9 (1:46pm will be ignored)

r, _ := w.Parse(text, time.Now())
// "2019-02-23 09:21:21.835182427 -0300 -03"
// 2019-02-23 (correct)
//   09:21:21 ("wrong")

// With custom distance (10):
	Distance:     10,
	MatchByOrder: true})

r, _ = w.Parse(text, time.Now())
// "2019-02-23 13:46:21.559521554 -0300 -03"
// 2019-02-23 (correct)
//   13:46:21 (correct)

State of the project

The project is in a more-or-less complete state. It's used for one project already. Bugs will be fixed as soon as they will be found.


  • readme: describe all the existing rules
  • implement missed rules for these examples
  • add cli and simple rest api server(#2)