获取JavaScript cookie过期时间值(分钟)可以通过以下步骤实现:
document.cookie
属性获取当前页面的所有cookie信息。下面是一个示例代码:
function getCookieExpirationTime(cookieName) {
var cookies = document.cookie.split(';');
var expirationTime;
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
if (cookie.indexOf(cookieName + '=') === 0) {
var cookieValue = cookie.substring(cookieName.length + 1);
var matches = cookieValue.match(/expires=([^;]+)/i);
if (matches) {
var expirationDateString = matches[1];
var expirationDate = new Date(expirationDateString);
var currentTime = new Date();
if (expirationDate > currentTime) {
var timeDifference = expirationDate.getTime() - currentTime.getTime();
expirationTime = Math.ceil(timeDifference / 60000); // 转换为分钟
}
}
break;
}
}
return expirationTime;
}
// 使用示例
var cookieName = 'exampleCookie';
var expirationTime = getCookieExpirationTime(cookieName);
console.log('过期时间(分钟):', expirationTime);
这段代码会在控制台输出目标cookie的过期时间(分钟数)。请将cookieName
替换为你要获取过期时间的cookie名称。
领取专属 10元无门槛券
手把手带您无忧上云