变量名称应具有描述性:
var date = new Date;
date.setTime(result_from_Date_getTime);
var seconds = date.getSeconds();
var minutes = date.getMinutes();
var hour = date.getHours();
var year = date.getFullYear();
var month = date.getMonth(); // beware: January = 0; February = 1, etc.
var day = date.getDate();
var dayOfWeek = date.getDay(); // Sunday = 0, Monday = 1, etc.
var milliSeconds = date.getMilliseconds();
给定月份的日期不变。 在a年,二月有29天。 受[http://www.javascriptkata.com/2007/05/24/how-to-know-if-its-a-leap-year/]的启发(感谢Peter Bailey!)
接续先前的代码:
var days_in_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// for leap years, February has 29 days. Check whether
// February, the 29th exists for the given year
if( (new Date(year, 1, 29)).getDate() == 29 ) days_in_month[1] = 29;
没有简单的方法来获取一年中的一周。 有关该问题的答案,请参阅javascript中是否可以使用年份和ISO周号创建日期对象?