wanasit / chrono

A natural language date parser in Javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parsing the input with respect to user timezone.

nerdprogrammergm opened this issue · comments

Hello, thanks for creating such an awesome API.
Presently, I am using this api for my project. I am running a node js application on heroku server.
I am facing a problem for parsing input with respect to timezone.
Suppose my user is in timezone IST(5.5) and send the time query to server "now" which is "2017-08-25T14:25:11.993662Z" according to my heroku server.
So what I want to do is parse the "now" and get the time with respect to user time zone.
Actually I am developing a Facebook chatbot and for that I am using this api.
Only thing I get to know about user is their timezone which is for India user on fb is 5.5
So how can I use this 5.5 to parse the input "now" with respect to user timezone.

Also if user send time query as "tomorrow at 5pm" then how can I get it as "2017-08-26T17:00:00.000Z" in IST
Please help.

Since the parsing is happening server site you're going to need to have the user's timezone saved already.

Once you know their timezone you should be able to use the timezoneOffset feature.

I ran into this issue myself. I had to pass refDate relative to user's timezone as second parameter to chrono.parse function. Here's the code snippet I'm using. I'm storing the user's timezone offset in minutes using momentjs in DB.

const parsedDate = chrono.parse(
    req.body.inputTimeText, //input text from user such as in 10 minutes, today at 11 AM etc.
    moment().utcOffset(req.user.utcTimezoneOffset) //From DB
)[0].start;

parsedDate.assign('timezoneOffset', req.user.utcTimezoneOffset);

const parsedMoment = moment(parsedDate.date())
    .utcOffset(req.user.utcTimezoneOffset);

const scheduleTime = parsedMoment.toISOString();
const message = parsedMoment.calendar(); // returns Today at 11 AM

@harshithkashyap Thanks for your solution.

I think chrono's interoperability with moment should be mentioned in the docs; very useful.

commented

@harshithkashyap's solution no longer works (ParsedComponents doesn't have an assign method). Is there another way of handling this?

commented

For future reference, this solved my issue for me: #327 (comment)