0

I'm setting the cookie in react using react-cookie

this.props.cookies.set("num_tag", num_tag, {
        expires: midnight
});

I wanted to reset the value of the cookie when it expires at midnight and not be null.

1 Answers1

0

From the documentation, you have to set the expires as an absolute date.

So what you can do is to get the current date, add one day to it and then push the time to midnight.

const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(0, 0, 0, 0);

this.props.cookies.set('num_tag', num_tag, {expires: tomorrow});
cross19xx
  • 3,170
  • 1
  • 25
  • 40
  • Hey I have implemented the same thing. My question is whether I can set a default value (not null) after it expires – Sugam Devare Feb 28 '20 at 10:17
  • Oh okay. You may have to use a conditional when retrieving the value. Other than that I'm not sure. But I'll be glad when you get a solution like that – cross19xx Feb 28 '20 at 10:20