I am using ngx-cookie-service for my angular app. In the official docs it says I can add a third parameter for the expiration time like -
this.cookieService.set('CookieName', 'Cookie Value', 5);
The third parameter is for expire time in days. Is there any way to set the expiration time of the cookie in minutes or in an hour? IE: 30 mint or Hour.
If you look at the code here: https://github.com/stevermeister/ngx-cookie-service/blob/master/projects/ngx-cookie-service/src/lib/cookie.service.ts#L105
You will see that expires is:
As such, you just need to get the current date/time and add an hour to it like so:
And then:
OR
Note, manipulating dates is much easier with a date manipulation library like
date-fnswhich always returns a JS Date.Obviously you will need to ... npm install date-fns ... for this to work. For alternative date manipulation libraries search for alternatives to
date-fnswith npm trends. Although, in my humble opinion, I considerdate-fnsto be top notch.Docs for date-fns are here