1.创建
- 语法:var newDate= new Date();
- 括号内没有参数:Date对象自动将当前的日期与时间保存
- 括号内为一个number类型的参数:表示与基准时间(1970-01-01 00:00:00.000GMT)相差的毫秒数
- 括号内是2个或2个以上number类型参数,按顺序分别表示年、月、日、时、分、秒、毫秒
- 括号内为一个String类型参数,如果格式可以识别,则获取String中的日期时间,例如"1999-02-23 11:22:33"
2.说明
- Date 类型具有自动调节至正确日期的能力,如:如果出现4月31日会自动调节为5月1日;
- Date 类型中月的存储"0"对应表示"1月"
3.方法
- get***(用于获取日期)
- set***(设置日期)
- toLocaleString() 根据本地时间格式将Date对象转换为字符串
举例
<script type="text/javascript"> var date=new Date(2016,5,22,23,22,00); //根据本地时间格式,将Date对象转换成字符串格式 console.debug(date.toLocaleString()); //get*** 获取Date对象的年月日时分秒 var year=date.getFullYear(); var month=date.getMonth();//得到的是5,但代表6月份 var day=date.getDate(); var hour=date.getHours(); var minute=date.getMinutes(); var second=date.getSeconds(); console.debug("year: "+year+",month: "+month+",day: "+day); console.debug("hour: "+hour+",minute: "+minute+",second: "+second); //set***设置Date对象的年月日等 var newdate=new Date(); newdate.setFullYear(1992); newdate.setMonth(1);//1代表的是2月 newdate.setDate(31); //31已经超出2月份的天数,自动调节为3月2日(92年为闰年) console.debug(newdate); </script>
后台结果: