Cookie 保存时效

1. dateadd法
1)将期限设置为当前日期后的第N天的0时0分0秒
Response.Cookies(“LastView”).Expires=dateadd(“d”,N,date)
2)将期限设置为当前日期后的第N月的同一天的0时0分0秒
Response.Cookies(“LastView”).Expires=dateadd(“m”,N,date)
3)将期限设置到当前时间后的第N天的该时该分该秒
Response.Cookies(“LastView”).Expires=dateadd(“d”,N,now)
4)将期限设置到当前时间后的第N+M天的该时该分该秒
Response.Cookies(“LastView”).Expires=dateadd(“d”,N,now+M)
5)将期限设置到当前时间后的第N月的该时该分该秒
Response.Cookies(“LastView”).Expires=dateadd(“m”,N,now)
6)将期限设置到当前时间后的第N月的同一日后的第M天的该时该分该秒
Response.Cookies(“LastView”).Expires=dateadd(“m”,N,now+M)

2. 确定日期法
1)将期限设置到某一日的0时0分0秒失效
举例:到2003年1月29日0时0分0秒失效
Response.Cookies(“LastView”).Expires=# 1/29/2003 #

Response.Cookies(“LastView”).Expires=# 2003/1/29 #

Response.Cookies(“LastView”).Expires=“January 29,2003”
2)将期限设置到某一日的某一时刻失效
举例:到2003年1月29日21时0分0秒失效
Response.Cookies(“LastView”).Expires = #2003/1/29 21:00:00#

Response.Cookies(“LastView”).Expires = #1/29/2003 21:00:00#

Response.Cookies(“LastView”).Expires = #January 29,2003 21:00:00#
其实,在这种方法中,用““ ””和“# #”的效果是一样的。例如
Response.Cookies(“LastView”).Expires=“January 29,2003”

Response.Cookies(“LastView”).Expires=# January 29,2003 # 的效果就是相同的。

3. date+ ow+ 法
这种方法主要结构为Date+数学式或now +数学式。通过简单的四则运算的方法,将Cookie 的失效时间设置到当前时间后的某一段时间内。这里的加号都表示在当前时间的基础上加上预设时间。
1)date+法
如将期限设置为当前日期后的第N天的0时0分0秒,我们可以使用上述已谈到过的方法也可以使用如下格式:
Response.Cookies(“LastView”).Expires=Date+N
date+法是一种比较死的方法。它和上述方法差别不是很大,完全可以由上述几种方法代替。下面介绍now+法,这是一种比较灵活的方法,它可以将失效时间准确地定位到当前时间后的任意一年、一月、一天、一小时、一分、一秒。
2)now+法
先举一个例子: Response.Cookies(“LastView”).Expires = now+1
这条语句的功能是:把Cookie的失效时间限制到1天后的同一时刻。这里的1代表1天,即24小时。由于now代表当前Web服务器的系统时间,包括年、月、日、时、分、秒,则该语句所表达得Cookie 的失效日期在明天的同一时、分、秒上。若改成now+2则表示此Cookie 的失效日期在两天后的同一时、分、秒上;now+30表示此Cookie 的失效日期在一个月后的同一时、分、秒上;now+30*2表示此Cookie 的失效日期在两个月后的同一时、分、秒上;now+365则表示此Cookie 的失效日期在一年后的同一时、分、秒上……
当把1分成24等份,即该语句变成: Response.Cookies(“LastView”).Expires = now+1/24则表示把Cookie的失效时间限制到一小时后。在此基础上,把1再分60等份,即把now+1/24改成 now+1/1440,则表示把Cookie的失效时间限制到一分钟后。这里的1440是由24*60得来。同理,要把Cookie的失效时间限制到十分钟后,则把程序变成Response.Cookies(“LastView”).Expires = now+10/1440;要把Cookie的失效时间限制到一秒钟后,则程序变成Response.Cookies(“LastView”).Expires = now+1/86400。当然,理论上把Cookie的失效时间限制到微秒级也是可以的。不过,那就没有什么实际意义了。另外,如果程序语句变成: Response.Cookies(“LastView”).Expires = now+1+1/1440则表示Cookie的失效时间为24小时零一分钟后。

原文地址:https://www.cnblogs.com/sarahVSEve/p/4465349.html