最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery - cookies set expired date for 1 day - Stack Overflow

programmeradmin5浏览0评论

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

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
Share Improve this question asked Oct 2, 2018 at 12:39 ZirekZirek 5334 gold badges12 silver badges22 bronze badges 1
  • Possible duplicate of Adding hours to Javascript Date object? – Andreas Commented Oct 2, 2018 at 12:41
Add a ment  | 

1 Answer 1

Reset to default 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
  });
发布评论

评论列表(0)

  1. 暂无评论