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 badges5 Answers
Reset to default 21000 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