How to use the chrono-node.parseDate function in chrono-node

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 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 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()
  }
}
github WorldBrain / Memex / src / overview / search-bar / components / DateRangeSelection.tsx View on Github external
parsePlainTextDate({ isStartDate }) {
        const stateKey = isStartDate ? 'startDateText' : 'endDateText'
        const dateState = isStartDate
            ? this.state.startDateText
            : this.state.endDateText

        const nlpDate = chrono.parseDate(dateState)

        analytics.trackEvent({
            category: isStartDate ? 'Overview start date' : 'Overview end date',
            action: nlpDate ? 'Successful NLP query' : 'Unsuccessful NLP query',
        })

        processEvent({
            type: isStartDate
                ? EVENT_NAMES.DATEPICKER_NLP_START_DATE
                : EVENT_NAMES.DATEPICKER_NLP_END_DATE,
        })

        // Get the time from the NLP query, if it could be parsed
        if (nlpDate != null) {
            return nlpDate.getTime()
        }
github clay / clay-kiln / lib / utils / timepicker.vue View on Github external
errorMessage() {
        if (_.get(this, 'args.validate')) {
          const validationError = getValidationError(this.timeValue, this.args.validate, this.$store, this.name),
            parsed = parseNaturalDate(this.timeValue);

          if (validationError) {
            return validationError;
          } else if (!parsed && !isEmpty(this.timeValue)) {
            // only display the time parsing error if the timepicker has data in it
            return `${this.help} (Please enter a valid time)`;
          }
        }
      },
      isInvalid() {
github probot / reminders / test / index.js View on Github external
expect(github.issues.edit).toHaveBeenCalledWith({
      number: 2,
      owner: 'baxterthehacker',
      repo: 'public-repo',
      labels: [{
        url: 'https://api.github.com/repos/baxterthehacker/public-repo/labels/bug',
        name: 'bug',
        color: 'fc2929'
      },
        'reminder']
    })

    const params = {
      who: 'baxterthehacker',
      what: 'check the spinaker',
      when: chrono.parseDate('July 1, 2017 9:00am')
    }

    expect(github.issues.edit).toHaveBeenCalledWith({
      owner: 'baxterthehacker',
      repo: 'public-repo',
      number: 2,
      body: `hello world\n\n`
    })

    expect(github.issues.createComment).toHaveBeenCalledWith({
      number: 2,
      owner: 'baxterthehacker',
      repo: 'public-repo',
      body: '@baxterthehacker set a reminder for **07/01/2017**'
    })
  })
github clay / clay-kiln / lib / utils / timepicker.vue View on Github external
update(val) {
        const parsed = parseNaturalDate(val);

        if (parsed) {
          this.$emit('update', dateFormat(parsed, 'HH:mm'));
        }
      },
      disableInput() {
github drewcummins / hodlol / src / models / types.ts View on Github external
public tryParseDateString(input:number | string):number {
        let startDate:Date = chrono.parseDate(input);
        if (startDate === null){
            logger.fatal("Bailing out, couldn't interpret scenario file start time", "start:", input);
        }
        else {
            return startDate.getTime();
        }
    }
github WorldBrain / Memex / src / util / nlp-time-filter.js View on Github external
export default function extractTimeFiltersFromQuery(query) {
    const matchedBefore = query.match(BEFORE_REGEX)
    const matchedAfter = query.match(AFTER_REGEX)

    let startDate
    let endDate
    if (matchedBefore) {
        const parsedDate = chrono.parseDate(matchedBefore[1])
        endDate = parsedDate && parsedDate.getTime()
    }
    if (matchedAfter) {
        const parsedDate = chrono.parseDate(matchedAfter[1])
        startDate = parsedDate && parsedDate.getTime()
    }

    const extractedQuery = query
        .replace(BEFORE_REGEX, '')
        .replace(AFTER_REGEX, '')
        .trim()

    return {
        startDate,
        endDate,
        query: extractedQuery,
    }
}
github macdja38 / pvpcraft / lib / TaskQueue.js View on Github external
schedule(task, time) {
    let datedTask = Object.assign({expireTime: this.db.r.epochTime(chrono.parseDate(time, Date.now()).getTime() / 1000)}, task);
    this.db.r.table("taskQueue").insert(datedTask).run();
  }
github drewcummins / hodlol / dist / models / types.js View on Github external
tryParseDateString(input) {
        let startDate = chrono.parseDate(input);
        if (startDate === null) {
            logger.fatal("Bailing out, couldn't interpret scenario file start time", "start:", input);
        }
        else {
            return startDate.getTime();
        }
    }
    dataDir() {

chrono-node

A natural language date parser in Javascript

MIT
Latest version published 2 months ago

Package Health Score

83 / 100
Full package analysis

Similar packages