2

I am using ngx-cookie-service for my angular5 app. any one help me to show the way to set the expire date?

I try like this:

setCookies($event){
        this.cookieService.set( 'retailAppCookies', "true", 30 );
        this.cookieService.set( 'expires', '2030-07-19' );
    }

But not works. any one help me?

Pavan Jadda
  • 4,306
  • 9
  • 47
  • 79
user2024080
  • 1
  • 14
  • 56
  • 96
  • Does this post addresses the same issue? I think the answer there could help you out. https://stackoverflow.com/questions/49817792/angular4x-ngx-cookie-service-with-expire-parameter – JeroenE Jun 13 '18 at 08:28

2 Answers2

1

you can not add date in ngx-cookie-service you have to add number of days like

 this.cookieService.set('CookieName', 'Cookie Value', 5);

this will expire after 5 days

Faisal
  • 584
  • 3
  • 11
  • 33
1

In 2021, you can add Date but it has be date instance. The following example sets cookie with 1 hour expiration

cookieService.set('test', 'Hello World', { expires: new Date(new Date().getTime() +  1000 * 60 * 60); });

See the API documentation for more details on this.

Pavan Jadda
  • 4,306
  • 9
  • 47
  • 79