Timestamp for 1st day of year
I am doing the following:
new Date().setFullYear(2011, 0, 1);
开发者_Python百科Does this gives me the number of seconds since January 1, 1970?
> new Date().setFullYear(2011, 0, 1);
Does this gives me the number of seconds since January 1, 1970?
No, because the hours, minutes and seconds need to be zeroed too:
x = new Date();
x.setFullYear(2011, 0, 1);
x.setHours(0, 0, 0);
alert(x - 0);
Internally, Javascript stores a date as the number of milliseconds since midnight, January 1, 1970. (Not seconds -- milliseconds.) You can get this number out of the date object with the getTime function.
In practice, the "base date" rarely makes a difference. Usually you use the various Date functions to format the date in a conventional format. You occasionally get differences between two times by using getTime and subtracting one from another.
That just gives you a date object.
Date d = new Date().setFullYear(2011, 0, 1);
d.getSeconds(); //gives you number of seconds for the date you've set (0-59), but not from Jan 1,1970.
精彩评论