How to use chrono-node - 10 common examples

To help you get started, we’ve selected a few chrono-node examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github glennjones / text-autolinker / lib / autolinker.js View on Github external
function addTimeTags(obj, options) {
	var data,
		iso,
		cn = options.className,
		restictedWords = ['today', 'tomorrow', 'yesterday'],
		tag = '',
		date = '',
		x = 0,
		i;


	if (options.publishedDate) {
		data = chrono.parse(options.text, options.publishedDate);
	} else {
		data = chrono.parse(options.text);
	}

	//console.log(JSON.stringify(data))

	if (data.length > -1) {
		i = data.length;
		while (x < i) {

			date = data[x].startDate;

			// do not use: 
			// impliedComponents for day, month
			// text containing today, tomorrow, yesterday
github zeke / remind-me / lib / parse.js View on Github external
patterns.some(function (_) {
    var pattern = easyPattern(_)
    if (pattern.test(input)) {
      result = pattern.match(input)

      // special case for trailing tomorrow
      if (!result.time && _.match(/tomorrow$/i)) {
        result.time = chrono.parse('tomorrow')[0].start.date()
      }

      if (result.interval) {
        result.interval = dateparser(result.interval).value
        result.time = Date.now() + result.interval
      }

      // undo the regex 'to' hack
      result.task = result.task.replace(/__to__/ig, ' to ')

      if (typeof result.time === 'string') {
        result.time = chrono.parse(result.time)[0].start.date()
      }

      return true
    }
github glennjones / text-autolinker / lib / autolinker.js View on Github external
function addTimeTags(obj, options) {
	var data,
		iso,
		cn = options.className,
		restictedWords = ['today', 'tomorrow', 'yesterday'],
		tag = '',
		date = '',
		x = 0,
		i;


	if (options.publishedDate) {
		data = chrono.parse(options.text, options.publishedDate);
	} else {
		data = chrono.parse(options.text);
	}

	//console.log(JSON.stringify(data))

	if (data.length > -1) {
		i = data.length;
		while (x < i) {

			date = data[x].startDate;

			// do not use: 
			// impliedComponents for day, month
			// text containing today, tomorrow, yesterday

			if (!data[x].start.impliedComponents) {
				data[x].start.impliedComponents = [];
github SerjoPepper / delorean_bot / src / node_modules / bot.js View on Github external
function getNotifyOptions (text, timezone) {
  var res;
  var resMoment;
  var options = {
    expireOffset: null, // expire offset in seconds
    unixTimestamp: null,
    text: text
  };
  try {
    res = chrono.parse(text);
  } catch (e) {
    console.error(e, e.stack);
    return null;
  }

  if (!res || !res.length) {
    return null;
  }

  res = res[res.length - 1];
  resDate = chrono.parseDate(res.text);

  if (res.tags.RUDeadlineFormatParser || res.tags.ENDeadlineFormatParser) {
    resMoment = moment(resDate);
    options.unixTimestamp = resMoment.unix();
    options.expireOffset = resMoment.diff(new Date, 'seconds');
github magda-io / magda / magda-search-api-node / src / createApiRouter.ts View on Github external
function parseDate(dateString: string, forward: boolean = false) {
        if (!dateString || dateString.trim().length === 0) {
            return undefined;
        }

        console.log(dateString);

        const parsed = chrono.parseDate(dateString, new Date(), {
            forwardDate: forward
        });

        console.log(parsed);

        return parsed;

        // We always use "start" because we're not actually trying to parse a date range out
        // of natural text
        // if (!parsed || parsed.length === 0 || !parsed[0].start) {
        //     return undefined;
        // }

        // return parsed[0].start.date();
    }
github unscrollinc / unscroll / client / src / components / Timeline / EventInput.js View on Github external
};
PositiveParser.extract = function(text, ref, match, opt) {
    console.log({ text: text, ref: ref, match: match, opt: opt });
    let pr = new chrono.ParsedResult({
        ref: ref,
        text: match[0],
        index: match.index,
        start: {
            year: parseInt(match[0], 10)
        }
    });
    const x = implyLateStart(pr);
    return x;
};

var custom = new en.Chrono();
custom.parsers.push(BCParser);
custom.parsers.push(NegativeParser);
custom.parsers.push(CenturyParser);
custom.parsers.push(PositiveParser);

class Timelist extends React.Component {
    constructor(props) {
        super(props);
        // update is a function that grabs the state
        this.editSeveral = props.editSeveral;

        this.state = {
            when_original: null,
            when_happened: null,
            resolution: null,
            parsed: null,
github gahabeen / jsonframe-cheerio / index.js View on Github external
}
		}

	} else if (["address", "add"].includes(extractor)) {
		result = addressit(data)
	} else if (["email", "mail", "@"].includes(extractor)) {
		if (multiple) {
			result = data.match(emailRegex) || data
			if (_.isArray(result) && result.length === 1) {
				result = result[0]
			}
		} else {
			result = data.match(emailRegex) !== null ? data.match(emailRegex)[0] : ""
		}
	} else if (["date", "d"].includes(extractor)) {
		let date = chrono.casual.parseDate(data)
		if (date) {
			result = date.toString()
		} else {
			result = ""
		}
	} else if (["fullName", "prenom", "firstName", "nom", "lastName", "initials", "suffix", "salutation"].includes(extractor)) {
		// compact data before to parse it
		result = humanname.parse(filterData(data, "cmp"))
		if ("fullName".includes(extractor)) {
			// return the object
		} else if (["firstName", "prenom"].includes(extractor)) {
			result = result.firstName
		} else if (["lastName", "nom"].includes(extractor)) {
			result = result.lastName
		} else if ("initials".includes(extractor)) {
			result = result.initials
github gahabeen / jsonframe-cheerio / modules / extractors.fn.js View on Github external
}
		}

	} else if (["address", "add"].includes(extractor)) {
		result = addressit(data)
	} else if (["email", "mail"].includes(extractor)) {
		if (multiple) {
			result = data.match(emailRegex) || data
			if (_.isArray(result) && result.length === 1) {
				result = result[0]
			}
		} else {
			result = data.match(emailRegex) !== null ? data.match(emailRegex)[0] : ""
		}
	} else if (["date", "d"].includes(extractor)) {
		let date = chrono.casual.parseDate(data)
		if (date) {
			result = date.toString()
		} else {
			result = ""
		}
	} else if (["fullName", "prenom", "firstName", "nom", "lastName", "initials", "suffix", "salutation"].includes(extractor)) {
		// compact data before to parse it
		result = humanname.parse(filterData(data, "cmp"))
		if ("fullName".includes(extractor)) {
			// return the object
		} else if (["firstName", "prenom"].includes(extractor)) {
			result = result.firstName
		} else if (["lastName", "nom"].includes(extractor)) {
			result = result.lastName
		} else if ("initials".includes(extractor)) {
			result = result.initials
github keystonejs / keystone / packages / arch / packages / day-picker / src / TextDayTimePicker.js View on Github external
function parseDate(value) {
  let [parsedDate] = chrono.parse(value);
  if (parsedDate === undefined) {
    return null;
  }
  let dateMoment = parsedDateToMoment(parsedDate);
  return dateMoment.toISOString(
    // passing true here keeps the offset in the iso string rather than
    // convert it to UTC which is the default to align with native JS Dates
    true
  );
}
github croqaz / clean-mark / lib / meta / rules / date.js View on Github external
return function ($) {
    let value = rule($)
    if (!value) return

    // remove whitespace for easier parsing
    value = value.trim()

    // convert isodates to restringify, because sometimes they are truncated
    if (isIso(value)) return new Date(value).toISOString()

    // try to parse with the built-in date parser
    const native = new Date(value)
    if (!isNaN(native.getTime())) return native.toISOString()

    // try to parse a complex date string
    const parsed = chrono.parseDate(value)
    if (parsed) return parsed.toISOString()
  }
}

chrono-node

A natural language date parser in Javascript

MIT
Latest version published 4 months ago

Package Health Score

80 / 100
Full package analysis

Similar packages