Skip to content

Commit ecb597b

Browse files
committed
Allow fractions of a day for expires option.
Closes js-cookie#246.
1 parent 2b5a3a3 commit ecb597b

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎jquery.cookie.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@
5252
var config = $.cookie = function (key, value, options) {
5353

5454
// Write
55+
5556
if (value !== undefined && !$.isFunction(value)) {
5657
options = $.extend({}, config.defaults, options);
5758

5859
if (typeof options.expires === 'number') {
5960
var days = options.expires, t = options.expires = new Date();
60-
t.setDate(t.getDate() + days);
61+
t.setTime(+t + days * 864e+5);
6162
}
6263

6364
return (document.cookie = [

‎test/tests.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ test('expires option as days from now', function () {
194194
'should write the cookie string with expires');
195195
});
196196

197+
test('expires option as fraction of a day', function () {
198+
expect(1);
199+
200+
now = new Date().getTime();
201+
expires = Date.parse($.cookie('c', 'v', { expires: 0.5 }));
202+
203+
// When we were using Date.setDate() fractions have been ignored
204+
// and expires resulted in the current date. Allow 1000 milliseconds
205+
// difference for execution time.
206+
ok(expires > now + 1000, 'should write expires attribute with the correct date');
207+
});
208+
197209
test('expires option as Date instance', function () {
198210
expect(1);
199211
var sevenDaysFromNow = new Date();

0 commit comments

Comments
 (0)