最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Purpose of date.getTime in the expires flag of the cookie? - Stack Overflow

programmeradmin7浏览0评论

I have a line of code that sets a cookie with an expiry date that looks like this.

var date = new Date();
date.setTime(date.getTime() + 1000*60*60*24*365);
var expires = "; expires=" + date.toGMTString();

What I'm trying to do is understand what each number represents. I know thats its just adding milliseconds to the time object but what does each one represent is the question.

I have a line of code that sets a cookie with an expiry date that looks like this.

var date = new Date();
date.setTime(date.getTime() + 1000*60*60*24*365);
var expires = "; expires=" + date.toGMTString();

What I'm trying to do is understand what each number represents. I know thats its just adding milliseconds to the time object but what does each one represent is the question.

Share Improve this question edited Mar 12, 2012 at 16:26 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Mar 12, 2012 at 16:21 ChapsterjChapsterj 6,63521 gold badges73 silver badges124 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

1000 milliseconds in a second

60 seconds in a minute

60 minutes in an hour

24 hour in a day

365 days in a year

So, you'll get the quantity of milliseconds in a year.

var date = new Date(); // date object (now on this puter)

date.setTime( // change the time
date.getTime() // now in milliseconds since 1970

+ 1000 // milliseconds in a second
* 60 // seconds in a minut
* 60 // minutes in an hour
* 24 // hours in a day
* 365 // approximate days in a year. Total ~ number of milliseconds in a year
);
var expires = "; expires=" + 
date.toGMTString(); // format the time to what the cookie likes

Please read

  • Date
  • toGMTString

for more information

1000 millicesonds times 60 seconds times 60 minutes times 24 hours times 365 days. It's a year.

Think about it like this:

1000*60*60*24*365

1000 // Converts milliseconds into seconds
60 // Converts seconds into minutes
60 // Convert minutes into hours
24 // Converts hours into days
365 // Convert days into year

1000 milliseconds per second, 60 seconds per minute, 60 minutes per hour, 24 hours in a day, 365 days in a year,

So it is adding the number of milliseconds in a year, advancing the time by a year

发布评论

评论列表(0)

  1. 暂无评论