wanasit / chrono

A natural language date parser in Javascript
MIT License
4.6k stars 341 forks source link

How to tell if result needs the time or not? #551

Closed surrealroad closed 5 months ago

surrealroad commented 6 months ago

If I parse a statement with "tomorrow", no specific time, how can I tell from the results that the time can be ignored? I'm using v1.4.8 if that matters

wanasit commented 5 months ago

Hello. I'm sorry for not getting back to you sooner.

In Chrono v2+, the parsed results consist of ParsedComponents that you can check for time certainty (and override it if needed).

const reference = new Date(2024, 5-1, 29, 9, 37);
const results = chrono.parse('I have an appointment tomorrow', reference);

// Get values
results[0].start.get('year') // 2024
results[0].start.get('month') // 5
results[0].start.get('day') // 30
results[0].start.get('hour') // 9  as implied same time as reference
results[0].start.get('minute') // 37  as implied same time as reference

// Get certainties
results[0].start.isCertain('year') // true
results[0].start.isCertain('month') // true
results[0].start.isCertain('day') // true
results[0].start.isCertain('hour') // false
results[0].start.isCertain('minute') // false

// Override via assign() or imply() methods
results[0].start.imply('hour', 0) // override to 0 (only if not certain)
results[0].start.imply('minute', 0) // override to 0 (only if not certain)

I cannot remember how things work in v1.x, but I believe the results in later version of v1 also work similarly.

surrealroad commented 5 months ago

It's exactly the same syntax. Thanks so much!