How to set expiry date in seconds for cookie in JSCookie - javascript

I'm using the JSCookie library to save and load cookies.
Now I want to save a cookie with an expiry date. This is officially supported with days like the documentation. But how can I set the expiry in seconds or minutes instead of days?
So I have the following code from the documentation but this is for example only for 7 days:
Cookies.set('name', 'value', { expires: 7, path: '' });
Are there any possibilities to achieve this?

RTFM...
var inFifteenMinutes = new Date(new Date().getTime() + 15 * 60 * 1000);
Cookies.set('foo', 'bar', {expires: inFifteenMinutes})

I've set the cookie the expire in 15 seconds.
function createCookie(name, value) {
var date = new Date();
date.setTime(date.getTime() + (15*1000));
var expires = "; expires= " + date.toGMTString();
document.cookie = name + "=" + value + expires + "; path=/";
}

Related

javascript setCookie expiration wrong or ignored by browser?

Question about cookie expiration.
I'm trying the setCookie from w3schools.com
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
console.log(expires);
}
when I try (note: today 2020-03-17...)
setCookie("test_cookie", "hello world", 363);
chrome dev says cookie
expires 2020-03-24....
but console.log I added says
expires=Wed, 15 Mar 2021
same thing happens with toGMTString
seems the browser ignores expires, and does 7 days...
so how to really set a cookie expiring in 1 month or 1 year ???

How to get time from date object without time zone in javascript?

I have Date object like
Date {Mon Aug 17 2015 05:45:23 GMT+0530 (IST)}
i want only time without GMT timezone and IST.I want only time like 05:45:23.thanks
You can use moment javascript library to format and process your dates.
The alternative option is manually accessing date hours, minutes and seconds.
Check the Date Reference.
You can build a string by using:
var formattedDate = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
Using pure JavaScript:
var d = new Date(),
hours = d.getHours(),
mins = d.getMinutes(),
secs = d.getSeconds(),
time = hours + ':' + mins + ':' + secs;
console.log(time);
You can create your own prototype extending the Date object.
Date.prototype.extractTime = function(){
var h = this.getHours();
var m = this.getMinutes();
var s = this.getSeconds();
return h + ":" + m + ":" + s;
}
Usage:
var d = new Date();
d.extractTime(); // outputs current time of your clock..

How to set multiple values with two differents expires dates

I'm trying to set cookie in javascript with two values.
Each of theses have a different expiration date.
For example :
var now = new Date();
now.setDate( now.getDate() + 2 );
document.cookie = "bar=foo;";
document.cookie = "expires=" + now.toUTCString() + ";"
now = new Date();
now.setDate( now.getDate() + 30 );
document.cookie = "foo=bar;";
document.cookie = "expires=" + now.toUTCString() + ";"
Is it correct?
How to set another value with an expiration date for 30 days for example?
I think that approach is correct.
Based on: How can I set a cookie to expire after x days with this code I have? :
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
Note, that setTime() and getTime() work on milliseconds.
And a few words from me: as javascript's Date sucks, I recommend using moment.js library when working with dates, it's brilliant.
Ok, i found my reply here with the function "setCookie". I've specify differents values and it's working.
http://www.w3schools.com/js/js_cookies.asp

Set a cookie expire after 2 hours

I have this JavaScript code:
function spu_createCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else
var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
How can I make the cookie expire after 2 hours?
If you want to use the same type of function, transform the days param into hours and pass 2 to get a 2 hour expiration date.
function spu_createCookie(name, value, hours)
{
if (hours)
{
var date = new Date();
date.setTime(date.getTime()+(hours*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else
{
var expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
Well -most obvious thing is to make "expire" date +2 hours ? :). Here You have nice prototype for that:
Adding hours to Javascript Date object?
Try this:
function writeCookie (key, value, hours) {
var date = new Date();
// Get milliseconds at current time plus number of hours*60 minutes*60 seconds* 1000 milliseconds
date.setTime(+ date + (hours * 3600000)); //60 * 60 * 1000
window.document.cookie = key + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
return value;
};
Usage:
<script>
writeCookie ("myCookie", "12345", 24);
</script>
//for 24 hours
Try jquery-cookie. Makes it very easy to work with cookies.
The following one-liner will set a cookie, name, with the value, value, and an expiration of two hours from the time of its creation. If the optional argument, days, is supplied, the cookie will expire after that many days instead.
Warning: there is no error-checking, so if mandatory parameters are omitted when called, or arguments are mistyped, the function will throw an error.
spu_createCookie = (name, value, days) => { document.cookie = `${name}=${value}; expires=${new Date(Date.now() + (days ? 86400000 * days : 7200000)).toGMTString()}; path=/` }
Relevant JavaScript syntax concepts:
Arrow Functions
An arrow function expression is a compact alternative to a traditional function expression, but is limited and can't be used in all situations.
Template Literals
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.
Ternary Operators
The conditional (ternary) operator is the only JavaScript operator that takes three operands ... This operator is frequently used as a shortcut for the if statement.
This would do it.
var now = new Date();
var time = now.getTime();
time += 7200 * 1000;
now.setTime(time);
document.cookie =
name+ '=' + value +
'; expires=' + now.toGMTString() +
'; path=/';

Can we get timezone of a user based on GMT

i came across this code which shows the current time of the user based on his timezone. Based on this can we tell if he is on IST or EST.plz help
<script type="text/javascript">
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10)
minutes = "0" + minutes
document.write("<b>" + hours + ":" + minutes + " " + "</b>")
</script>
i wanted to display the time along with timezone.for example 17:35 IST
var now = new Date();
localtime = now.toTimeString();
which will return something like "12:21:44 GMT-0400 (EDT)"
You cannot tell what timezone he is in - just what time they are in based on GMT - use this useful function to get your info though -> http://www.pageloom.com/automatic-timezone-detection-with-javascript

Categories

Resources