angularangular5ngx-cookie-service

How to set the cookie expire date with `ngx-cookie-service`


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?


Solution

  • 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.