angular.js - How to set a cookie to expire after 24:00 today in angularjs?
phpcn_u15822017-05-15 16:55:13
0
2
784
The cookie stores a piece of user participation information. This information is expected to be valid within today and expire after 24:00. How to set it?
What I say below are all operated in Angular 1.4.x version.
1. First you need to load the ngCookies module, and then add this dependency where you need it. 2. Then you can refer to $cookies here for specific methods. 3. The general method is as follows:
4. The cookie time can be set by yourself. You can set some options through $cookiesProvider. For details, see $cookiesProvider here. 5. I have a small example here that you can take a look at, demo 6. The specific code is as follows:
The cookie time is set on the server side, not on the web page. The server side is simple. Just calculate the expiration time. If it is Java, it is recommended to use joda time. The following is an example of using joda time to set the 24-point expiration:
DateTime now = DateTime.now();
DateTime endOfToday = now.withTimeAtStartOfDay().plusDays(1);
Cookie cookie = new Cookie(key, value);
cookie.setMaxAge(Seconds.secondsBetween(now, endOfToday).getSeconds());
cookie.setDomain(domain);
cookie.setPath("/");
response.addCookie(cookie);
1. First you need to load the
ngCookies
module, and then add this dependency where you need it.2. Then you can refer to $cookies here for specific methods.
3. The general method is as follows:
4. The cookie time can be set by yourself. You can set some options through
$cookiesProvider
. For details, see $cookiesProvider here.5. I have a small example here that you can take a look at, demo
6. The specific code is as follows:
The cookie time is set on the server side, not on the web page. The server side is simple. Just calculate the expiration time. If it is Java, it is recommended to use joda time. The following is an example of using joda time to set the 24-point expiration: