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

4.1k Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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.

0
On

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