How to set multiple values with two differents expires dates - javascript

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

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 ???

Format GMT date to integer in PHP

I'm creating a cookie in JavaScript with this code. I actually changed the code a bit:
function setCookie (name,value,days) {
var expires, newValue;
if (days) {
var date = new Date(); // days = 0.0006944444; // testing with one minute
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toString();
newValue = encodeURIComponent(value)+'|'+date+expires;
} else expires = "";
document.cookie = name+"="+(newValue)+"; path=/";
}
So the above function sends encodeURIComponent(value)+'|'+date+expires as value. In PHP I can do explode('|',$_COOKIE['my-key']) with the date formatted like this:
$string_time = "Fri Oct 06 2017 19:34:44 GMT 0300 (Eastern European Summer Time);
Now I need to convert this string to integer to be compared against the PHP's time() integer format.
Doing the following:
$currentTime = date('YmdHis', time());
$expire_time = date('YmdHis', strtotime($string_time));
It actually outputs this:
string(14) "19700101000000" // $currentTime
string(14) "20171006162139" // $cookie_time
Question why is $currentTime always the same 19700101000000 value?
Just use a unix timestamp instead, as you're not getting the time from the expries settings, but from the cookies value
function setCookie (name,value,days) {
var expires, newValue;
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toUTCString();
newValue = date.getTime() / 1000;
} else {
expires = "";
}
document.cookie = name+"="+(newValue)+"; path=/";
}
Now you can compare it directly to the PHP unix timestamp from time() and get the difference in seconds.
Note that you're not even using the expires variable, so this does nothing when it comes to how long the cookie is valid.

How to set expiry date in seconds for cookie in JSCookie

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=/";
}

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=/';

javascript date increment

I want to set a text box with a date (in dd/mm/yyyy format) 14 days ahead to current date in javascript . can any one help me regarding this ?
This should do it:
var myDate=new Date();
myDate.setDate(myDate.getDate()+14);
then
document.getElementById(YOUR_TEXTBOX_ID).value = myDate.getDate() + "/" +
(myDate.getMonth() + 1) + "/" + myDate.getFullYear();
Date.js is a handy script for all kinds of JavaScript date manipulation. I've used it to make many date-based interfaces, including calendar controls.
Like Deodeus suggested, use Date.js:
var myDate = Date.today().add(14).days();
document.getElementById('mytextbox').value = myDate.toString('dd/MM/yyyy');
Following is the function to increment date by one day in javascript.
function IncrementDate(date) {
var tempDate = new Date(date);
tempDate.setDate(tempDate.getDate() + 1);
return tempDate;
}
Function calling...
var currentDate = new Date();
var IncrementedDate = IncrementDate(currentDate);

Categories

Resources