How to find the start and end date with now-1w/w using momentjs

I am passing the selected time range to the node application where i receive the req as follows { from: 'now-1w/w', to: 'now-1w/w' }.I need to extract the from and to date from now-1w/w. I have tried using moment-relativisim plugin to convert the grafana time range to date and time.but it returns both the from and date as same that is from: 27-04-2021T22:30:00 and to: 27-04-2021T22:30:00 Instead of from:21-04-2021T00:00:00 and to: 27-04-2021T23:59:59

The “from” and “to” strings you posted are the same, so it stands to reason that the time stamps would the the same also. Should “from” be now-2w/w instead?

EDIT: I see based on the docs of the module that Grafana does some magic here, which the module doesn’t. Afraid I personally don’t have any great ideas how to solve that

EDIT 2: based on the docs, it looks like you need to substitute the / in the from string with a |, in order to round down rather than up.

@svetb No, If we choose previous week from the time range i am getting the from and to date as ‘now-1w/w’


Yeah makes sense. I updated my comment, also with a suggested solution based on the docs of the module you’re using.

@svetb Yeah it works fine now by changing / to |. Thanks for the help

1 Like

Cool, glad it worked @nidhinkumar. I’d never noticed that Grafana parses “rounded” timestamps differently depending on whether it’s a “from” or “to” timestamp! Learn something new every day…

1 Like