Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
How to set expire time of Cookie using `ngx-cookie-service`

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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:

Number of days until the cookies expires or an actual Date

As such, you just need to get the current date/time and add an hour to it like so:

const myDate: Date = new Date();
myDate.setHours( myDate.getHours() + 1 );

And then:

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

OR

this.cookieService.set('CookieName', 'Cookie Value', { expires: myDate });

Note, manipulating dates is much easier with a date manipulation library like date-fns which always returns a JS Date.

import { addHours } from 'date-fns';
const myDate: Date = addHours(new Date(), 1);

Obviously you will need to ... npm install date-fns ... for this to work. For alternative date manipulation libraries search for alternatives to date-fns with npm trends. Although, in my humble opinion, I consider date-fns to be top notch. J-E-S-U-S loves you :D

Docs for date-fns are here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda