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
1 Answer
Reset to default 324 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
});