1

I'm trying to learn about cookies in js, followed one tutorial and everything went smoothly but I wish to set expired date for 1 day. If I just write {expired: 1} It. somehow is 22h, I found on forum example like :

var date = new Date();
            var expired = '';
            date.setTime(date.getTime() + 1);
            expired += date.toGMTString();

But It doesn't really work for me and doesn't show cookies at all when I try to do

{expires: expired}

Can you guys give me some hints how to set it for 24hours?

$('#accept').click(function () {
        if (!$('.change-message--on-click').is('hide--first')) {
            $('.change-message--on-click').removeClass('hide--second');
            $('.change-message--on-click').addClass('hide--first');

            var date = new Date();
            var expired = '';
            date.setTime(date.getTime() + 1);
            expired += date.toGMTString();

            $.cookie('choosen-Accept', 'yes', {expires: 1 });
        }
    return false
Zirek
  • 513
  • 3
  • 11
  • 20
  • Possible duplicate of [Adding hours to Javascript Date object?](https://stackoverflow.com/questions/1050720/adding-hours-to-javascript-date-object) – Andreas Oct 02 '18 at 12:41

1 Answers1

3

24 hours is 24 * 60 * 60 * 1000 milliseconds. Look at this

If you need add few days to date of expires cookie you should use number

$.cookie('choosen-Accept', 'yes', {
   expires: 1
});

If you need custom time you should create date and pass it to expired

var date = new Date();
date.setTime(date.getTime() + 24 * 60 * 60 * 1000);
$.cookie('choosen-Accept', 'yes', {
    expires: date
});

To make sure you can look at this source code line 6

Full example

  $('#accept').click(function() {
    if (!$('.change-message--on-click').is('hide--first')) {
      $('.change-message--on-click').removeClass('hide--second');
      $('.change-message--on-click').addClass('hide--first');

      var date = new Date();
      date.setTime(date.getTime() + 24 * 60 * 60 * 1000);

      $.cookie('choosen-Accept', 'yes', {
        expires: date
      });
    }
    return false
  });
Sergey-N13
  • 260
  • 2
  • 9
  • It works, I guess:)! thank you so much, but could you only tell me, before in expires, was written particular date and hours when session is expired, now it is "at the end of session", is it normal and will it be expired after 24h or I did something wrong? Plus with var expired = date.toGMTString(); line it didn't work – Zirek Oct 02 '18 at 13:01
  • Sorry I don't understand your question. Could you please more information about what you want to do ? – Sergey-N13 Oct 02 '18 at 13:41
  • I wish to set expires date for 24hours, if I write above code with line `var expired = date.toGMTString();`, it doesn't work, if without this line, It does work. However when I check it in console log, it shows number 1538574304616, I assume that if before `{expires:1}` meant one day(or rather weird 22h), then if I pass this gigantic number, it will mean almost forever, no? – Zirek Oct 02 '18 at 13:47
  • I expanded my example for you. If you use `{expires:1}` your cookies will expires after 1 day. If you need use custom time you need to use `{expires: data}` when type of data is Date – Sergey-N13 Oct 02 '18 at 14:00
  • You can look at [source code](https://github.com/carhartl/jquery-cookie/blob/master/src/jquery.cookie.js.). `line: 62` – Sergey-N13 Oct 02 '18 at 14:02
  • 1
    oki I got it now and it works:) Thanks friend for your help and patience:) – Zirek Oct 02 '18 at 14:03