Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery cookie过期时间不正确_Javascript_Jquery - Fatal编程技术网

Javascript jQuery cookie过期时间不正确

Javascript jQuery cookie过期时间不正确,javascript,jquery,Javascript,Jquery,我使用jquery设置cookie。 但consoleLog日期和过期日期不一样 function setCookie() { const date = new Date(); //Tue Oct 22 2019 17:45:53 GMT+0900 (한국 표준시) const expires = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59); // Tue Oct 22 2019

我使用jquery设置cookie。 但consoleLog日期和过期日期不一样

function setCookie() {   
const date = new Date(); //Tue Oct 22 2019 17:45:53 GMT+0900 (한국 표준시)   
const expires = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59); // Tue Oct 22 2019 23:59:59 GMT+0900 (한국 표준시) 
$.cookie('AAA', '', { expires }); 

}
但到期日期为2019-10-22T14:59:59.000Z 时间不一样。 我在Chrome中发现了这个问题。
非常感谢你的帮助

简单答案

当您将非UTC日期放入expires时,javascript会自动将该日期转换为GMT

JavaScript Cookie expires time must be GMT/UTC
您可以使用以下命令来设置UTC日期,该日期在23:59:59结束

const date=新日期();
const expires=新日期(Date.UTC(Date.getFullYear(),Date.getMonth(),Date.getDate(),23,59,59));

log(expires.toutString())我认为到期日期没有问题,因为新日期以UTC+0时间表示,如“Z”符号所示,相当于您指定的时间。consoleLog日期为2019年10月22日星期二23:59:59 GMT+0900。实际上cookie设置过期时间是2019-10-22T14:59:59.000Z。时间不一样您可以尝试将
警报(expires.toISOString())
与实际到期日期进行比较,它将产生相同的结果。我将此代码转换为const expires=新日期(date.getFullYear()、date.getMonth()、date.getDate()、23、59、59).toISOString();但错误正在起作用,“uncaughttypeerror:options.expires.toutString不是一个没有帮助的函数”。而是创建一个在UTC时间23:59:59结束的日期。@zuuuu我添加了使用
date.UTC
方法的代码,这是您需要的。享受:)const expires=Date.UTC(Date.getFullYear(),Date.getMonth(),Date.getDate(),23,59,59);这是UTC代码吗??但cookie expires正在设置会话,而不是date@zuuuuuuuu是的,它以UTC为单位构造一个日期并返回毫秒。因此,我们可以进一步将其用于
newdate()
以获取日期对象
const expires=新日期(Date.UTC(Date.getFullYear(),Date.getMonth(),Date.getDate(),23,59,59))
是您需要的完整代码吗