Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing the input with respect to user timezone. #214

Open
nerdprogrammergm opened this issue Aug 25, 2017 · 5 comments
Open

Parsing the input with respect to user timezone. #214

nerdprogrammergm opened this issue Aug 25, 2017 · 5 comments

Comments

@nerdprogrammergm
Copy link

nerdprogrammergm commented Aug 25, 2017

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.

@RayBB
Copy link

RayBB commented Aug 30, 2017

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.

@harshithkashyap
Copy link

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

@nVitius
Copy link

nVitius commented Sep 3, 2018

@harshithkashyap Thanks for your solution.

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

@erbridge
Copy link

erbridge commented Sep 7, 2020

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

@erbridge
Copy link

erbridge commented Sep 7, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants